サービスがあるアプリケーションがあります。サービスは、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();
}
}
}
番号をブロックしますか? –
"サービス停止"と言えば、あなたの活動終了後にそのonDestroy()が呼び出されるということですか?また、「終了申請」と言いますと、その活動を終了しただけですか? – SirKnigget
はいSystem.exit(0)でメインアクティビティを終了します。 – user2331641