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


- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01