2014-04-17 1 views
0

Je commence un service STICKY dans mon mainActivity # create, mais lorsque l'utilisateur appuie sur les boutons System Home ou Back, le service n'est pas déserté .... en tous cas ? ne devrait-il pas être COLLANT?Android comment arrêter un service quand l'utilisation a frappé le dos de système ou le bouton principal?

MainActivity.java 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
     ... 
     startService(new Intent(this, FeedbackService.class)); 


    FeedbackService.java 
    public class FeedbackService extends Service implements OnInitListener { 
    …. 
@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     //Log.i(TAG, "Received start id " + startId + ": " + intent); 
     return START_STICKY; // run until explicitly stopped. 
    } 
+0

Avez-vous un appel de méthode stopService? –

Répondre

4

Cela confirmera l'utilisateur veut quitter avant qu'il ne tue le service.

@Override 
public void onBackPressed() { 
    final DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(final DialogInterface dialog, final int which) { 
      switch (which) { 
       case DialogInterface.BUTTON_POSITIVE: 

        super.onBackPressed(); 
        stopService(intentName) 

       case DialogInterface.BUTTON_NEGATIVE: 
        //No button clicked 
        break; 
      } 
     } 
    }; 

    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Are you sure you exit and stop the background process?").setPositiveButton("Yes", dialogClickListener) 
      .setNegativeButton("No", dialogClickListener).show(); 
} 
+1

Merci beaucoup c'est ce dont j'ai besoin ..... marche bien – erwin

+0

Les plus bienvenus m8 –

0

Remplacer la méthode OnPause() et ajoutez ci-dessous le code sur OnPause()

stopService(intent_name); 
+0

bon point, j'oublie toujours l'onPause !!! thenak – erwin

Questions connexes