Switch off GPS programmatically

时光怂恿深爱的人放手 提交于 2019-12-19 19:28:34

问题


What i have: Currently my app is giving location through gps.

what i want: Gps to turn off automatically after i exit from the application. Because it keeps on telling me the location time and again that looks odd and also gps consume a lot battery.


回答1:


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.




回答2:


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);



回答3:


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.




回答4:


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)


来源:https://stackoverflow.com/questions/6925832/switch-off-gps-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!