How to know when the services has ended

335 views Asked by At

I want know when the service has ended, so I use BroadcastReceiver. My service name is CheckNuevosAvisosIntentServices and I launch it in main.java (onCreate method) as:

        Intent msgIntent = new Intent(Main.this, CheckNuevosAvisosIntentService.class);
        msgIntent.putExtra("iteraciones", 1);
        startService(msgIntent);

My manifest inside application tag.

<!-- Services -->
<service android:name="com.kirolm.instalacionesdep.services.CheckNuevosAvisosIntentService" />

In another fragment (HomeFragment) I use isMyServiceRunning method and BroadcastReceiver class:

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (CheckNuevosAvisosIntentService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

My Broadcast code is this:

public class ProgressReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_PROGRESO)) {

            Log.e("Testing", "The service is running...");


        }
        else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN) && isAdded()) {
            Log.e("Testing", "The service has been ended");
        }
    }
}

and in creathe method (HomeFragment) I implements this:

            if(!isMyServiceRunning()){
                Log.e("Testing", "HomeFragment: The service is running");

            }else{
                Log.e("Testing", "HomeFragment: The servie stop");
            }

CheckNuevosAvisosIntentServicescode:

public class CheckNuevosAvisosIntentService extends IntentService{

    public static final String ACTION_PROGRESO = "com.kirolm.instalacionesdep.services.action.PROGRESO";
    public static final String ACTION_FIN =  "com.kirolm.instalacionesdep.services.action.FIN";


    public CheckNuevosAvisosIntentService() {
        super("CheckNuevosAvisosIntentService");
        // TODO Auto-generated constructor stub

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub

        int iter = intent.getIntExtra("iteraciones", 0);

        for(int i=1; i<=iter; i++) {

            Intent bcIntent = new Intent();
            bcIntent.setAction(ACTION_PROGRESO);
            bcIntent.putExtra("progreso", i*10);
            sendBroadcast(bcIntent);
        }
        buscaNuevasNoticasRss();
        Intent bcIntent = new Intent();
        bcIntent.setAction(ACTION_FIN);
        sendBroadcast(bcIntent);

    }



    private void buscaNuevasNoticasRss() {
        // TODO Auto-generated method stub
        //This method checks.  When this method finish I want finish my services.
    }
}

When buscaNuevasNoticiasRssfinish, I want finish my services.

I receive when the service is running but I don't receive when the service stop.

Edited: HomeFragment (onCreat method)

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

                ProgressReceiver = new BroadcastReceiver() {

                    @Override
                    public void onReceive(Context context, Intent intent) {
                        // TODO Auto-generated method stub
                        Log.e("Testing", "HomeFragment. BoradcastReceiver.  Dentro de onReceive");

                        if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_PROGRESO)) {

                            Log.e("Testing", "HomeFragment. BoradcastReceiver. The service is running...");


                        }
                        else if(intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN) && isAdded()) {
                            Log.e("Testing", "HomeFragment. BoradcastReceiver. The service has been ended");


                        }

                    }
                };

}
1

There are 1 answers

5
Kartheek On BEST ANSWER

Override the onDestroy() method of your IntentService and send a broadcast to your receiver saying the service is destryed.

public class CheckNuevosAvisosIntentService extends IntentService{
   //Your code
      .......
      .......
 @Override
    public void onDestroy() {
        super.onDestroy();
        sendBroadcast(new Intent(CheckNuevosAvisosIntentService.ACTION_FIN));
    }
}

And create a BroadcastReceiver as an inner class of HomeFragment.

  private class HomeFramgnet extends Fragment {
        private ProgressReceiver progressReceiver;
        Override

        public void onCreate(Bundle savedInstanceState) {
            progressReceiver = new ProgressReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(CheckNuevosAvisosIntentService.ACTION_PROGRESO);
            intentFilter.addAction(CheckNuevosAvisosIntentService.ACTION_FIN);
            registerReceiver(progressReceiver, intentFilter);
        }

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

        class ProgressReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                Log.e("Testing", "HomeFragment. BoradcastReceiver.  Dentro de onReceive");

                if (intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_PROGRESO)) {

                    Log.e("Testing", "HomeFragment. BoradcastReceiver. The service is running...");


                } else if (intent.getAction().equals(CheckNuevosAvisosIntentService.ACTION_FIN) && isAdded()) {
                    Log.e("Testing", "HomeFragment. BoradcastReceiver. The service has been ended");


                }

            }
        }
    }