Switch off GPS programmatically

橙三吉。 提交于 2019-12-01 19:02:28

Looking at above comment thread it seems its possible to turn OFF GPS programatically (But seeing only 12 Upvotes)

But if you switch OFF GPS from your application programatically, what if other applications use GPS Service ?

The solution would be like

Open Settings of android and then tell user to turn OFF GPS when he exits from the application...

For this you can do like :

Intent i = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);

OR

You can try like

locationManager.removeUpdates(myLocationListener); 
locationManager = null;

This shutdown gps for this app, but its still available for use by other apps.

This code can alter your gps.But it is not documented

 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")); 
            sendBroadcast(poke);

Even though the link Rasel posted contains some code which might work you should keep in mind that the code is exploiting a security flaw which has been fixed already (http://code.google.com/p/android/issues/detail?id=7890) and therefore shouldn't work in the near future anymore.

If you want to enable the GPS programmaticaly then copy and paste this code in your project, its working fine.

      final Intent i = new Intent();
      i.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider"); 
      i.addCategory(Intent.CATEGORY_ALTERNATIVE);
      i.setData(Uri.parse("3")); 
       sendBroadcast(i); //if you are in broad cast receiver activity then use context.sendBroadcast(i)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!