Programmatically turn ON GPS in android(以编程方式在android中打开GPS)
问题描述
可能的重复,
如何启用/禁用 gps 和以编程方式在android中移动数据?
我一直看到人们告诉他们能够以编程方式在 android 中打开 GPS.但我使用的是相同的代码,但无法做到这一点.
它只是显示正在搜索 GPS ..",但实际上并没有这样做.
这是我正在使用的代码,
//启用GPSIntent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("启用", true);发送广播(意图);//禁用GPSIntent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("启用", false);发送广播(意图);我也试过了,
String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);if(!provider.contains("gps")){//如果gps被禁用最终意图戳=新意图();poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");poke.addCategory(Intent.CATEGORY_ALTERNATIVE);poke.setData(Uri.parse("3"));context.sendBroadcast(戳);}但当我这样做时,
LocationManager manager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);boolean isGPSEnabled = 经理.isProviderEnabled(LocationManager.GPS_PROVIDER);它总是将 isGPSEnabled 返回为 false.如果我手动打开 gps,它会返回 true.
任何人都可以告诉我这样可以打开 GPS 吗?如果是这样,我哪里出错了.
//开启GPSIntent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("启用", true);context.sendBroadcast(意图);//禁用GPSIntent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("启用", false);context.sendBroadcast(意图);<块引用>
在广播时使用上下文.
希望这有效..
要启用移动数据,您应该使用它 -
final ConnectivityManager conman = (ConnectivityManager) 上下文.getSystemService(Context.CONNECTIVITY_SERVICE);最终类 conmanClass = Class.forName(conman.getClass().getName());最终字段 iConnectivityManagerField = conmanClass.getDeclaredField("mService");iConnectivityManagerField.setAccessible(true);最终对象 iConnectivityManager = iConnectivityManagerField.get(骗子);最终类 iConnectivityManagerClass = 类.forName(iConnectivityManager.getClass().getName());最终方法 setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);setMobileDataEnabledMethod.setAccessible(true);setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);Possible duplicates,
How to enable/disable gps and mobile data in android programmatically?
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.
It is just showing that "Searching for GPS .." but not actually doing it.
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 isGPSEnabled as false always. If I turn on the gps manually it is returned as true.
Can any body tell me is it possible to ON the GPS like this? If so, where I'm going wrong.
//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);
这篇关于以编程方式在android中打开GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式在android中打开GPS
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
