我正在尝试创建一个每五分钟发送一次用户位置更新的应用程序.我想我的代码工作得很好,但是我收到有关应用程序正在使用的权限的错误.我很确定我已在清单文件中添加了权限.有人能告诉我什么是错的吗?这是我的代码.
MainActivity.java
LocationManager locationManager ;
String provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting LocationManager object
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider,5*60*1000,0,this);
if(location!=null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
} …Run Code Online (Sandbox Code Playgroud) 我有一个扩展Fragment的类并实现了LocationListener.当我写作
LocationManager myLocalManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Run Code Online (Sandbox Code Playgroud)
我得到一个编译时错误,因为该方法getSystemService不是Fragment的方法.
我能做些什么来创造LocationManager?
总的来说,我对 Kotlin 协程和 Android 开发很陌生。在尝试了解它是如何工作的时,我遇到了一个似乎无法解决的错误。
从基本活动中,我尝试连接到 googleApiClient。权限没问题。我希望使用 kotlin 协程以直接方式从 LocationManager 获取位置更新,以便稍后使用此 Location 对象。我第一次在模拟器中改变我的位置时它工作正常,第二次我改变我的位置时,它崩溃了,异常如下:
FATAL EXCEPTION: main
Process: com.link_value.eventlv, PID: 32404
java.lang.IllegalStateException: Already resumed, but got value Location[gps 48.783000,2.516180 acc=20 et=+59m16s372ms alt=0.0 {Bundle[mParcelledData.dataSize=40]}]
at kotlinx.coroutines.experimental.AbstractContinuation.resumeImpl(AbstractContinuation.kt:79)
at kotlinx.coroutines.experimental.AbstractContinuation.resume(AbstractContinuation.kt:72)
at com.link_value.eventlv.View.Create.NewEventLvActivity$await$2$1.onLocationChanged(NewEventLvActivity.kt:100)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:297)
at android.location.LocationManager$ListenerTransport.-wrap0(LocationManager.java)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:242)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_event_lv)
askForUserLocation()
val locationManager = this.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val presenter = CreateEventPresenterImpl(this@NewEventLvActivity)
googleApiClient = GoogleApiClient.Builder(this@NewEventLvActivity)
.enableAutoManage(this /* …Run Code Online (Sandbox Code Playgroud) 我使用NETWORK_PROVIDER来获得latitude和longitude的地方.
我已经检查了"位置和安全性"中的设置并启用了"使用无线网络".但"isProviderEnabled(LocationManager.NETWORK_PROVIDER)"总是回来false.
谁能帮我?先感谢您!
这是我的代码:
LocationManager locManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isEnableGPS=locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isEnableNTW=locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.d(TAG, isEnableGPS+", "+isEnableNTW);
Run Code Online (Sandbox Code Playgroud)
AndroidMainfest.xml中的权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud) 这个问题与" Android:获取用户的当前位置而不使用gps或互联网 " 相同的主流stackoverflow问题直接相关,其中接受的答案实际上没有回答问题.
我应该能够通过网络提供商而不是GPS或互联网获取设备的当前位置名称(例如:城市名称,村庄名称).以下是该问题的公认答案.(以下代码部分应包含在onCreate()方法中)
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener …Run Code Online (Sandbox Code Playgroud) 干净利落.我开始一项活动并检查手机是否启用了GPS模块.如果未启用,我会通过对话框提示用户,并询问他是否要手动启用它.在是,我激活位置设置.现在用户可以根据需要启用它,但我需要检查他做了什么.
try {
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {}
if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Your GPS module is disabled. Would you like to enable it ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// Sent user to GPS settings screen
final ComponentName toLaunch = new ComponentName(
"com.android.settings",
"com.android.settings.SecuritySettings");
final Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(toLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 1); …Run Code Online (Sandbox Code Playgroud) 我在Android中的onLocationChanged事件有问题.这是触发:
case R.id.start: {
Points.add(overlay.getMyLocation()); // Points' type is ArrayList<GeoPoint>
mgr.requestLocationUpdates(best, 0, 3, locationListener);
}
break;
Run Code Online (Sandbox Code Playgroud)
这是onLocationChanged方法:
public void onLocationChanged(Location location) {
i++;
Points.add(overlay.getMyLocation());
MapOverlay mapOverlay = new MapOverlay(Points.get(i-1), Points.get(i));
map.getOverlays().add(mapOverlay); //does the drawing
mMapController.animateTo(Points.get(i));
}
Run Code Online (Sandbox Code Playgroud)
因此,onLocationChanged仅在我按"start"后调用一次.它应该在每次位置发生变化时自动调用,对吗?就我而言,事实并非如此.
请帮我.
我有一个应用程序,它使用网络提供程序的位置.每次应用程序启动时,它都会检查是否使用LocationManager中的isProviderEnabled()方法启用了网络提供程序.如果返回false,则向用户发出警报以启用网络提供程序,然后使用该应用程序.这个逻辑一直运行得非常好,除了一些非例外的非Google认证设备(因为它们通常也没有Maps API).最近,对于ICS上的一些设备,现在在JellyBean模拟器上,即使启用了isProviderEnabled(),我也会得到一致的"假".
我已经转移到使用从Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED)返回的字符串,以查看它是否包含"网络".这是一个黑客,但它现在正在工作.我希望能够使用isProviderEnabled()方法.
以前有人看过这个问题吗?
我正在使用Google API和MapActivity,MapView等.
当我需要获取当前位置时,我会在第一时间使用此代码:
myLocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Sets the criteria for a fine and low power provider
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
crit.setPowerRequirement(Criteria.POWER_LOW);
// Gets the best matched provider, and only if it's on
String provider = myLocationManager.getBestProvider(crit, true);
// If there are no location providers available
if (provider == null){
OnClickListener listener = new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Closes the activity
finish();
}
};
Utils.getInstance().showAlertDialog(this, R.string.warning_dialog_title,
R.string.no_location_providers_error_message,
android.R.drawable.ic_dialog_alert, false, listener);
}
// Location …Run Code Online (Sandbox Code Playgroud) 在三星Galaxy S2(GT-i9100),Android版本4.3上测试应用程序时,我有这个奇怪的堆栈跟踪.如果它有帮助,Bugsense报告"日志数据"= {u'ms_from_start':u'19915',u'rooted':u'true'},所以我不太确定这个设备是否有根(客户端)正在测试应用程序,而不是我).编辑:当我输入此信息时,客户确认我该设备正在使用自定义ROM,如果它很重要.
无论如何,这是一个完整的堆栈跟踪:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackagename/com.mypackagename.activities.ARActivity}:
java.lang.SecurityException: invalid package name: com.google.android.gms
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5303)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: invalid package name: com.google.android.gms
at android.os.Parcel.readException(Parcel.java:1431)
at android.os.Parcel.readException(Parcel.java:1385)
at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:540)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:836)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:430)
at android.privacy.surrogate.PrivacyLocationManager.requestLocationUpdates(PrivacyLocationManager.java:290)
at com.mypackagename.activities.ARActivity.onCreate(ARActivity.java:371)
at android.app.Activity.performCreate(Activity.java:5259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1098)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
Run Code Online (Sandbox Code Playgroud)
现在,ARActivity.java:371正在调用
locationManager.requestLocationUpdates(GPS, gpsRefreshPeriod, 0, locListener);
Run Code Online (Sandbox Code Playgroud)
哪里
private String GPS = "gps";
private …Run Code Online (Sandbox Code Playgroud) android ×10
locationmanager ×10
gps ×3
java ×2
location ×2
fragment ×1
geolocation ×1
kotlin ×1