2016-10-06 8 views
0

アプリがバックグラウンドタスクから強制終了した場合でも、私のサービスを実行したい。誰も私にいくつかの提案をお願いします。 (細かい作業、私はアプリを最小化する)アプリが殺されたときにサービスを実行するには?

ここに私のコード:

public class MyService extends Service { 
    private BroadcastReceiver mReceiver = null; 
    public MyService() { 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 
    @Override 
    public void onCreate() { 
     final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     mReceiver = new ScreenReceiver(); 
     registerReceiver(mReceiver, filter); 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
      if (ScreenReceiver.wasScreenOn) { 
      Log.e("MYAPP", "SCREEN TURNED OFF"); 

      } else { 
      // this is when onPause() is called when the screen state has not changed 
     } 
     return START_STICKY; 
     } 

    @Override 
    public void onDestroy() { 
      super.onDestroy(); 
    } 
} 
+0

ブロードキャストレシーバーを使用して意図を検出し、サービスを開始することができます –

+0

例がありますか? – help

+1

あなたのサービスの 'onDestroy()'で放送を送信し、 'BroadcastReceiver'を作成(登録)して、サービスが殺された場合に' broadcast'を得ることができます。しかし、実際に悪い習慣は、ユーザーによって殺されているサービスを再起動する......決して公開するつもりのアプリのためにこれを行う必要があります... – Opiatefuchs

答えて

0

あなたのサービスが開始されている場合は、それがシステムまたはユーザー Hope this will help you

+0

私はすでにしていますその – help

2

によって殺されたら、サービスを再起動する必要がありますあなたのアプリケーションでは、実際にあなたのサービスはメインプロセスで実行されています。アプリが殺されると、サービスも停止されます。次はあなたの放送受信機のコード

import android.app.*; 
import android.content.*; 
import android.os.*; 

public class ScreenReciever extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent background = new Intent(context, yourservice.class); 
     context.startService(background); 
    } 

} 

public class MyService extends Service { 
    private BroadcastReceiver mReceiver = null; 
    public MyService() { 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 
    @Override 
    public void onCreate() { 
     final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     mReceiver = new ScreenReceiver(); 
     registerReceiver(mReceiver, filter); 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
      if (ScreenReceiver.wasScreenOn) { 
      Log.e("MYAPP", "SCREEN TURNED OFF"); 

      } else { 
      // this is when onPause() is called when the screen state has not changed 
     } 
     return START_STICKY; 
     } 

    @Override 
    public void onDestroy() { 
      super.onDestroy(); 
    } 

     @Override public void onTaskRemoved(Intent rootIntent){ 
       Intent intent = new Intent("com.android.ServiceStopped"); 
sendBroadcast(intent); 
     } 

をされて、再びサービスを開始します放送受信機を持っている:だから何であるか、あなたが行うことができ、次のようにあなたのサービスのonTaskRemoved方法からブロードキャストを送信します。私はそれを試した。サービスはすべてのタイプの殺害から再開します。

+0

あなたは私のコードでどうしたらいいですか? – help

+0

@helpしばらくお待ちください – eLemEnt

+0

ありがとうございました – help

関連する問題