私はユーザに通知するインテントサービスを持っています。アプリがアクティブであるか、それがバックグラウンドにあるときでも、状態を停止した場合Android、IntentServiceがアプリを終了すると死ぬ
@Override
protected void onHandleIntent(final Intent workIntent) {
for(int i = 0; i < 10; i++){
try{
Thread.sleep(2000);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(String.valueOf(i) + "times called!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(i, mBuilder.build());
}catch(InterruptedException ex){}
}
}
このアプローチは完璧に動作します:次のようにそのonHandleIntentです。
しかし、私は(左にスワイプ)アプリケーションを殺すと、サービスの実行が停止します。まだ終了していない場合でも。どのように私はこれを克服できますか?
ありがとう、本当にトリックをしました。しかし、もう一つ質問があります。 forループを削除してtry/catchブロックを実行すると、コードが機能しません。それはなぜでしょうか? – QnARails