我設置了一個AlarmManager對象來啓動服務。
由AlarmManager啓動的服務不會從活動中停止
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); //context = getApplicationContext();
Intent alarmServiceIntent = new Intent(context, AlarmHandlingService.class);
alarmServiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmServiceIntent.putExtra("alarmId", _id); //_id is a "long" value
PendingIntent pi = PendingIntent.getService(context, (int)_id, alarmServiceIntent, 0);
am.set(AlarmManager.RTC_WAKEUP, time, pi);
AlarmHandlingService
的onStartCommand(..., ..., ...)
的作品,因爲它有工作,當它有工作。沒有任何問題:
public int onStartCommand(Intent intent, int flags, int startId)
{
long alarmId = intent.getLongExtra("alarmId", -1);
Intent someActivityIntent = new Intent(this, SomeActivity.class);
someActivityIntent.putExtra("alarmId", alarmId);
someActivityIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(someActivityIntent);
return START_NOT_STICKY;
}
但是,當一個新的活動打開(SomeActivity)和我打電話stopService(...)
,該服務沒有停止:
stopService(new Intent(context, AlarmHandlingService.class)); //context = getApplicationContext();
這是申報在清單我的服務類:
<service android:name="***myPackageName***.AlarmHandlingService"></service>
我試圖調試,發現只有的android.content.ContextWrapper
CLAS的registerReceiver (BroadcastReceiver receiver, IntentFilter filter)
功能當我撥打stopService
時,s被稱爲。
你的onDestroy()中有什麼? – NetStarter
我對onDestroy無能爲力。不是在stopService之後調用嗎?並且我的stopService根本沒有被調用... @覆蓋 public boolean stopService(意圖名稱) { Log.e(TAG,「Service is stopped」); return super.stopService(name); } – smb
Aaaaaa !!!!謝謝你非常非常,NetStarter :)我將代碼從stopService移動到onDestroy,現在它的工作:) – smb