Android service s'arrête quand je l'ai sortie de l'application

J'ai une application avec un service. Le Service est démarré par l'application dans le principal de l'activité sur la méthode de création. Mon problème survient lorsque je l'ai sortie de l'application: le service s'arrête et redémarre parfois immédiatement, parfois redémarre ces derniers temps. Je l'ai testé en version 4.1.2 et la version 2.3.6. le service n'est pas nouveau dans la version 2.3.6. Mon approche de mal? Entre l'arrêt et le redémarrage de fois, l'appel qui doit bloqués peuvent venir. Est-il possible de ne pas arrêter le service lors de l'application est quittée?
Note: je ne peux pas utiliser un service distant.

j'ai ajouté startForeground de service, mais le même problème. lorsque je quitte l'application l'arrêt du service, et de ne pas redémarrer la version 2.3.3 même je vois notification.Mais phoneStateListener prend la valeur null
Comment puis-je m'assurer quand j'ai quitter l'application, le service ne s'arrête pas

 //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()
{

    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);  
}

- je changer la où la définition callstatelistener de service de diffusion en tant que Dhawal Sodha conseillé, mais dans ce cas de blocage d'appel non sollicité devenir lent. je pense que sur la diffusion TelephonyManager initialise chaque appel. La diffusion de code ci-dessous. Lorsque j'utilise le service,
lorsque l'activité principale est quitté le service s'est arrêté dans la version 2.3.3. Une idée ? De radiodiffusion ou d'un service ? Comment faire diffuser plus rapidement? chaque appel des variables de l'objet et de l'initialiser à nouveau dans la diffusion.

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();
        }
    }   
}
  • vous souhaitez numéro de bloc??? puis il suffit d'utiliser ce code dans la Diffusion de recevoir.. au lieu de démarrer le service..
  • Quand vous dites "s'arrête", tu veux dire que son onDestroy() est appelée après la fin de votre activité? Aussi, quand vous dites "quitter l'application", entendez-vous en train de terminer l'activité?
  • oui la finition de l'activité principale du Système.exit(0);
  • stackoverflow.com/questions/16651009/...
  • j'ai ajouté une réponse sur ce problème ici
InformationsquelleAutor user2331641 | 2013-04-29