是否可以以编程方式打开设备上的LocationProviders(GPS提供程序/网络提供程序)(如果已关闭)?在Android中以编程方式打开位置提供程序
回答
不,它不是, 但你可以打开位置服务设置窗口:
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
使用下面的代码来启动GPS
locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
locationListener = new LocationListener();
locationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0, 0, locationListener);
和下面的代码停止GPS
locationManager.removeUpdates(locationListener);
详情请参阅documentations for LocationManager和for LocationListener。
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
但这个代码只支持高达8 APK
是的,但你必须是一个超级用户(须藤),您的设备必须植根。
与模式的高精确度或节约耗电量,而无需用户需要访问设置
http://android-developers.blogspot.in/2015/03/google-play-services-70-places-everyone.html
从Google Play Services 7.0开始,这应该是被接受的答案。您可以在不离开应用的情况下进行应用更新设置。参见上面的链接。 –
这个链接并没有说太多的实现,我错过了什么? –
执行示例在这里:http://stackoverflow.com/questions/33251373/turn-on-location-services-without-navigating-to-settings-page – MorZa
随着最近棉花糖更新,即使位置设置打开启用位置,您的应用程序将需要明确要求允许。推荐的方式是显示应用的权限部分,其中用户可以根据需要切换权限。这样做的代码片段如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Permission");
builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app.");
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.setNegativeButton(android.R.string.no, null);
builder.show();
}
} else {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isGpsProviderEnabled, isNetworkProviderEnabled;
isGpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGpsProviderEnabled && !isNetworkProviderEnabled) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Permission");
builder.setMessage("The app needs location permissions. Please grant this permission to continue using the features of the app.");
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton(android.R.string.no, null);
builder.show();
}
}
并重写为下面的onRequestPermissionsResult
方法:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "coarse location permission granted");
} else {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
}
}
}
另一种方法是,你还可以使用SettingsApi询问其位置提供(多个)启用。如果没有启用,您可以提示对话框更改应用程序中的设置。
- 1. 有没有可能在android中以编程方式打开GPS提供程序?
- 2. 以编程方式将MOXy设置为JAXB提供程序
- 3. 以编程方式设置活动会员提供程序
- 4. 以编程方式设置ASP.Net角色提供程序
- 5. 以编程方式在android中打开前置摄像头
- 6. Android屏幕以编程方式打开
- 7. Monkeyrunner以编程方式打开设置
- 8. android位置TextViews以编程方式
- 9. Android - 以编程方式打开消息应用程序
- 10. 以编程方式打开设置应用程序(iPhone)
- 11. GPS位置提供程序在Android中给我空位置
- 12. 以编程方式打开位置服务视图
- 13. 以编程方式在android中设置编辑文本提示?
- 14. 如何在我的android应用程序中以编程方式打开GPS?
- 15. 以编程方式提供AWS账户?
- 16. Android通知以编程方式打开横幅设置
- 17. 以编程方式打开Android通知设置
- 18. Bouncycastle以编程方式安装提供程序
- 19. 以编程方式添加成员资格提供程序
- 20. 如何在android中以编程方式打开SMS窗口?
- 21. 以编程方式在Android中打开一个CSV文件
- 22. 如何在android中以编程方式打开移动设备?
- 23. 如何在android中以编程方式打开文件夹?
- 24. android在2.2中以编程方式打开和关闭GPS?
- 25. 以编程方式在Android中打开屏幕
- 26. 如何以编程方式在Food Mode中打开Android相机?
- 27. 如何在Android中以编程方式打开目录
- 28. 在android中以编程方式打开和关闭屏幕
- 29. 如何以编程方式在Android中打开/关闭通知?
- 30. 如何以编程方式在ios中打开Internet设置?
我正在谈论以编程方式打开android设备(设置 - >位置和安全 - >使用无线网络/使用GPS卫星)选项。 –