在我的應用程序中,我有一個廣播接收器,它在收到SMS消息中的關鍵字後開始跟蹤手機GPS位置的服務。我做到這一點使用 -如何從廣播接收器中取消註冊聽衆和停止服務
context.startService(new Intent(context,TrackGPS.class));
我還需要能夠在在SMS消息接收另一個關鍵字停止服務,我試圖做到這一點,但GPS傳感器仍然跟蹤定位和GPS圖標閃爍時屏幕的頂部。我試圖這樣做 -
context.stopService(new Intent(context, TrackGPS.class));
我想這可能是因爲GPS偵聽器需要註銷。任何人都可以幫助我獲得這項服務,在收到文字後停止+停止GPS跟蹤?提前致謝。
解決方案
@Override
public int onStartCommand(Intent GPSService, int flags, int startId) {
boolean startListening = GPSService.getExtras().getBoolean("IsStartTracking");
if (startListening == true){
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else {
lm.removeUpdates(this);
stopSelf();
}
return 1;
}
搞亂了這個方法之後,我弄明白了,你是傳奇!我會在上面發佈我的代碼,因爲onStart方法已被棄用。 – bobby123 2011-01-25 18:16:17