嗨,我正在创建一个防盗应用程序,并通过短信定位我的手机,它的工作完美,直到2.3。 但在4.0我不能以编程方式打开或关闭gps是否有任何其他可能的方式来通过代码开关gps。我怎样才能启用禁用GPS在编程方式在Android?
27
A
回答
41
请尝试使用此代码。它适用于所有版本的我。
public void turnGPSOn()
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.ctx.sendBroadcast(intent);
String provider = Settings.Secure.getString(ctx.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"));
this.ctx.sendBroadcast(poke);
}
}
// automatic turn off the gps
public void turnGPSOff()
{
String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
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"));
this.ctx.sendBroadcast(poke);
}
}
+0
非常感谢... – 2013-03-15 08:24:22
+0
此代码打开我的设备中的GPS,它不工作在关闭我正在使用4.0.4 ics – 2013-03-15 09:11:14
+2
嗨它不是在三星galaxy y工作。它把GPS接收机在常开模式,得到修复后,它应该是在睡眠模式.. – Sandeep 2013-06-02 10:00:26
相关问题
- 1. 如何在rooted android上以编程方式禁用/启用gps?
- 2. 如何在Android 4.4中以编程方式启用/禁用GPS?
- 3. 我怎样才能获得转弯方向使用gps坐标或waypoints编程方式在android
- 4. 我怎样才能在android
- 5. 我们怎样才能在sim卡中以编程方式添加短信android
- 6. 如何以编程方式在android中启用/禁用gps和移动数据?
- 7. 如何在Android 6.x上以编程方式启用或禁用GPS?
- 8. 我怎样才能在Android上调用Facebook应用程序?
- 9. 我怎样才能以编程方式获得macbook的型号?
- 10. 如何以编程方式在android手机中禁用GPS?
- 11. 无法以编程方式在Android中禁用GPS
- 12. android-我怎样才能使用“android:layoutDirection”?
- 13. 我怎样才能在android系统
- 14. 我怎样才能在Android 4.0的上
- 15. 我怎样才能在Android的
- 16. 我们怎样才能在Android 1.6的
- 17. 我怎样才能恢复在android
- 18. 我怎样才能得到在Android的
- 19. 我怎样才能在Android模拟器
- 20. 我怎样才能在Android的
- 21. 我怎样才能在Android的
- 22. Android我怎样才能在线程上使用Realm
- 23. 我怎样才能在同步方法中使用新线程
- 24. 我怎样才能用ajax
- 25. 我怎样才能用jQuery
- 26. 我怎样才能在JSP形式
- 27. 我怎样才能在Ionic2
- 28. 我怎样才能在Zebble
- 29. 我怎样才能在PreferenceScreen
- 30. 我怎样才能在vim
http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker有看的代码在这里 – Dilip 2013-03-15 06:31:11
过去PIC在你的问题。 – dhams 2013-04-10 06:06:36
您是否检查过此链接http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker? – Jitendra 2013-04-10 06:08:27