[英]Location turned on but Listener returns nothing
I need to get user Location using location manager.我需要使用位置管理器获取用户位置。 I successfully use intent to start Settings.ACTION_LOCATION_SOURCE_SETTINGS.
我成功地使用意图启动 Settings.ACTION_LOCATION_SOURCE_SETTINGS。 I turn on location but still cant't get location.
我打开位置,但仍然无法获取位置。
When i checked phone settings and turned on App Level Permissions my app it worked fine.But i don't want users to manually do that,how can they turn on locations once and have app level permission for the app also turned on当我检查手机设置并打开应用程序级别权限时,我的应用程序运行良好。
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
LocationRequest request = new LocationRequest();
request.setInterval(0);
// Build the alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Services Not Active");
builder.setMessage("Please enable Location Services and GPS");
builder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
// Show location settings when the user acknowledges the
alert dialog
Intent intent = new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(EmployeeDashBoard.this,"You must turn on
Location",Toast.LENGTH_LONG).show();
startActivity(new Intent(EmployeeDashBoard.this,
LoginScreen.class));
}
});
Dialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}else{
//listener that responds to location updates
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateEmployeeLocation(location);
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
}
location turned on but no location got when i turn on app level permission it works.How can users do both at once
位置已打开但当我打开应用级别权限时没有位置
I assume you are asking for LOCATION_PERMISSION only in Manifest and not during run time.我假设您只在清单中而不是在运行时要求 LOCATION_PERMISSION。
After Android 6.0 (API level 23) , You have to ask permission during run time.在 Android 6.0(API 级别 23)之后,您必须在运行时请求许可。
if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,Manifest.permission.READ_CONTACTS)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
} }
The above code is to ask permission about READ_CONTACTS Do some modification to above code and add it to your project, it should work.上面的代码是询问READ_CONTACTS的权限 对上面的代码做一些修改并添加到你的项目中,它应该可以工作。 Go through this Documentation .
浏览本 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.