2011-12-22 132 views
1

我用我的應用GPS,我想開啓GPS和關閉編程以節省電能我怎麼能做到這一點 :(android在2.2中以編程方式打開和關閉GPS?

這是關掉我需要請

之交
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(); 
    } 
} 
+0

Ÿ沒有想到abbt accpting的答案 – drooooooid 2011-12-22 09:07:22

+1

可能重複[啓用GPS編程喜歡塔斯克(http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker) – 2011-12-22 09:29:31

回答

-2

可以使用

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
LocationListener locationListener = new CTLocationListener(); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener); 

對GPS轉而使用關閉它

locationManager.removeUpdates(locationListener); 

或者你也可能會發現此另一個線程上使用的GPS: How can I enable or disable the GPS programmatically on Android?

+0

感謝大家對於幫助 :) – 1093822 2011-12-23 08:39:51

+0

不適用於Android 2.3.4「CTLocationListener()」 – 2012-08-06 00:50:25

+0

這不會關閉Gps,它要求您的應用程序接收GPS更新,然後它會要求您的應用程序不再接收更新。 – FutureSci 2013-10-26 11:53:22

1
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(); 
    } 
} 
1
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(); 
       } 

,如果你正在選項爲false,可以禁用GPS。希望這可以幫助你。