2016-10-19 2 views
0

を実行しているサービスを維持、私はこのコードを使用:そのサービスは、着信SMSをリッスンするようになっている私は、ユーザーが削除されるまで、バックグラウンドで実行するサービスを記述しようとしている無期限

Intent activityIntent = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
      activityIntent, 0); 

    Notification notification = new Notification.Builder(this). 
      setContentTitle(getText(R.string.app_name)). 
      setContentText("Doing stuff"). 
      setContentInfo(""). 
      setSmallIcon(R.drawable.icon). 
      setDeleteIntent(createOnDismissedIntent(getBaseContext(), notId)). 
      setContentIntent(pendingIntent).build(); 



    notificationManager.notify(notId,notification); 

    return START_REDELIVER_INTENT; 

とそれがSMSを検出すると何かをしてください。 これはうまくいきますが、しばらく待ってからサービスがまだ起動しているかどうかをSMSで確認しようとすると、サービスがダウンしていることを意味するものはありません。 「START_REDELIVER_INTENT」を使用した場合、サービスが停止するのはなぜですか? - サービス内のonDestroy機能の通知を削除します。だからしばらくしてからサービスが停止しても、サービスが破壊されていないことを示す通知がまだ残っています

+1

Aサービスは、他のアプリのための部屋を作るために、いつでも殺さすることができます。システムは最終的に再起動を試みますが、使用できないシナリオを処理する必要があります。 – DeeV

+0

ありがとう、ありがとう。 – javaLovah

答えて

1

再起動後に自動再起動サービスが必要です。

マニフェスト:また

<service android:exported="false" android:name=".service.YourService" android:enabled="true"></service> 
    <receiver android:name=".service.YourBootReceiver"> 
     <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 

許可:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

受信機を定義します。

public class YourBootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     Intent serviceIntent = new Intent(arg0, YourService.class); 
     arg0.startService(serviceIntent); 
    } 
+0

あなたの答えをありがとう、しかし私の主な質問はなぜサービスがしばらく後に停止するのだろうか? – javaLovah

+0

私は後でyoutrコードをチェックしますが、私は私の使用とその大丈夫、私のサービスは常に働いています。 – josedlujan

関連する問題