2013-04-29 24 views
6

サービスがあるアプリケーションがあります。サービスは、createメソッドの主なアクティビティでアプリによって開始されます。私の問題は、アプリケーションから終了するときに発生します:サービスが停止し、すぐに再起動することがあります。私はバージョン4.1.2とバージョン2.3.6でテストしました。サービスはバージョン2.3.6では再開しません。私のアプローチは間違っていますか?停止時間と再開時間の間に、ブロックする必要があるコールが送信されます。アプリが終了したときにサービスを停止しない方法はありますか? 注:リモートサービスは使用できません。アプリケーションを終了するとAndroidサービスが停止する

サービスにstartForegroundを追加しましたが、同じ問題が発生しました。私は、アプリケーションを終了すると、サービスが停止しても、私はnotification.But phoneStateListener iがアプリケーションを終了するときにどのように私は確保できるNULL値 を取る参照、サービスは(

//application main metod 
@Override 
protected void onCreate(Bundle savedInstanceState) {    
//..... 

    startService(new Intent(getApplicationContext(), MyPhoneStateListener.class)); 
} 




@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    setCallListener();  
    setNoti(); 
    return START_STICKY; 
} 

private void setCallListener() 
{ 
     try 
     { 
      if (phoneStateListener==null) 
      { 

       phoneStateListener = new StateListener(); 
       telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
       telephonymanager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE); 
      } 
     } 
     catch(Exception ex) 
     { 

     } 
    } 

ます。private void setNotiを停止していないバージョン2.3.3を再起動しません) {

Notification notification= new Notification(R.drawable.ic_launcher, getResources().getString(R.string.app_name), System.currentTimeMillis()); 

    Intent main = new Intent(this, MainActivity.class); 

    main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT); 

    notification.setLatestEventInfo(this, getResources().getString(R.string.app_name), getResources().getStringArray(R.array.blockstate)[Sabitler.ShieldState].toString(), pendingIntent); 

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR; 

    startForeground(123456, notification); 
} 

iはどこDhawal Sodhaが遅くなる迷惑電話を遮断することをお勧めしますが、この場合のように放送するサービスからcallstatelistenerを設定変更します。私は放送TelephonyManagerはすべての呼び出しを初期化すると思います。以下の放送コード。私がサービスを利用するとき、主な活動が終了したとき バージョン2.3.3でサービスが停止しました。何か案が ?放送またはサービス?ブロードキャストをより速くするにはすべての呼び出し変数とオブジェクトはブロードキャストで再び初期化されます。

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{ 

    MyCustomStateListener myCustomStateListener; 
    TelephonyManager telephonymanager; 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     try 
     { 
      //Log.e("receiver1",String.valueOf(System.currentTimeMillis())); 

      telephonymanager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE); 
      myCustomStateListener = new MyCustomStateListener(context,telephonymanager); 
      telephonymanager.listen(myCustomStateListener, PhoneStateListener.LISTEN_CALL_STATE); 
      //Log.e("receiver2",String.valueOf(System.currentTimeMillis())); 
      telephonymanager.listen(myCustomStateListener, PhoneStateListener.LISTEN_NONE); 
     } 
     catch(Exception ex) 
     { 
      Toast.makeText(context,"setCallListener:"+ex.toString(), Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
+0

番号をブロックしますか? –

+0

"サービス停止"と言えば、あなたの活動終了後にそのonDestroy()が呼び出されるということですか?また、「終了申請」と言いますと、その活動を終了しただけですか? – SirKnigget

+0

はいSystem.exit(0)でメインアクティビティを終了します。 – user2331641

答えて

6

startServiceでサービスを開始していますか? その場合、アクティビティを終了すると、サービスは何にもバインドされず、システムによって停止することができます。

アクティビティに関係なくバックグラウンドサービスを実行する場合は、startForegroundを使用し、一定の通知を行います。 System.exit(0)とあなたのアプリを仕上げhttp://developer.android.com/reference/android/app/Service.html

UPDATE

は、サービスリファレンスを参照してください? もちろん、あなたのサービスは止まり、それはあなたのプロセスを殺します。 そのようなアプリケーションを終了することは絶対にありません。ちょうどfinish()あなたの活動です。

+0

私は、Dhawal Sodhaがアドバイスしたように、callstatelistenerの設定をサービスからブロードキャストに変更しますが、この場合は不要なコールをブロックするのが遅くなります。私は – user2331641

+0

感謝SirKniggetだと思います。 – user2331641

+0

答えがあなたの問題を解決した場合は、それを正しいものとしてマークしてください。 – SirKnigget

7

は、それはあなたのサービスを停止しないであろう、あなたのonStartCommand()メソッド

showToast("onStart"); 

    Intent intent = new Intent(this, NotStickyActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    Notification noti = new Notification.Builder(getApplicationContext()) 
       .setContentTitle("Pratikk") 
       .setContentText("Subject") 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentIntent(pendingIntent) 
       .build(); 

    startForeground(1234, noti);  

    return Service.START_STICKY; 

にこのコードを追加します。

+0

アプリケーションを終了すると、通知バーから通知が却下されたこのサービスも停止します –

関連する問題