问题
i am using GPS in My application and i want to turn GPS on and off Programmatically to save power how can i do it :(
this is for turn off i need the turn on please
private void turnGPSOnOff(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){
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);
//Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
}
}
回答1:
You can turn on the gps using
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new CTLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);
and turn it off using
locationManager.removeUpdates(locationListener);
Or you might also find this another thread on gps of use: How can I enable or disable the GPS programmatically on Android?
回答2:
private void turnGPSOnOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ // for turn on
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);
Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
}
}
回答3:
try
{
dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
}
catch (SecurityException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (NoSuchMethodException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
dataMtd.setAccessible(true);
try {
dataMtd.invoke(conm,true);
//gprDisable();
}
catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InvocationTargetException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
and if you are making the options to false, you can disable the GPS. Hope this may help you.
来源:https://stackoverflow.com/questions/8600731/android-turn-gps-on-off-programmatically-in-2-2