2014-05-15 83 views

回答

1

设置通过AlarmManager报警每10分钟。在警报中,为GPS提供者调用locationManager.requestSingleUpdate。这将打开GPS足够长的时间来获得一个修复程序,然后关闭它(除非其他程序正在使用它)。当它得到修复时,它会通过您在requestSingleUpdate中注册的回调来调用您。然后你可以做任何你需要的结果(比如通过AsyncTask发送给服务器)。

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


} 
} 

//关闭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

他想得到的位置,而不是打开和关闭GPS设置 –

+0

好吧,亲爱的...他写道,他想在获得位置后关闭gps ... @ GabeSechan – chiragkyada

相关问题