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:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01