在android中以编程方式打开GPS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20740365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically turn ON GPS in android
提问by tejas
Possible duplicates,
可能的重复,
How to enable/disable gps and mobile data in android programmatically?
如何以编程方式在android中启用/禁用gps和移动数据?
I have been seeing people telling that they are able to turn ON the GPS in android programatically. But I'm using the same code and not able to do that.
我一直看到人们告诉他们可以以编程方式在 android 中打开 GPS。但是我使用相同的代码并且无法做到这一点。
It is just showing that "Searching for GPS .." but not actually doing it.
它只是显示“正在搜索 GPS ..”,但实际上并没有这样做。
Here is the code I'm using,
这是我正在使用的代码,
//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
I also tried this,
我也试过这个,
String provider = Settings.Secure.getString(context.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"));
context.sendBroadcast(poke);
}
But when I do,
但是当我这样做时,
LocationManager manager = (LocationManager) getSystemService( getApplicationContext().LOCATION_SERVICE );
boolean isGPSEnabled = manager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
it returns me isGPSEnabledas false always. If I turn on the gps manually it is returned as true.
它总是将我的isGPSEnabled返回为 false。如果我手动打开 GPS,它会返回为 true。
Can any body tell me is it possible to ON the GPS like this? If so, where I'm going wrong.
任何人都可以告诉我是否可以像这样打开 GPS?如果是这样,我哪里出错了。
回答by Amit
//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
context.sendBroadcast(intent);
//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
context.sendBroadcast(intent);
use context when you broadcast.
广播时使用上下文。
hope this works..
希望这有效..
To enable mobile data you should use this -
要启用移动数据,您应该使用此 -
final ConnectivityManager conman = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass
.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField
.get(conman);
final Class iConnectivityManagerClass = Class
.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass
.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);