Getting the current status of process/service anyt

2019-08-11 02:19发布

I have made the intent service and its all working fine. I have made the interfaces and made call backs. What call back does , it receives 3 following messages :

  1. Service Start: When this message arrives, my "Start Service" button changes into "Stop Service" button.
  2. Service Finished: when this message arrives my button gets disappear
  3. Error: when this message arrives my button change into the "Error" button.

The call back is working perfectly. now I have following problems noticed when testing. these are as under

Problem:

When I have started the service by clicking the "Start Service" button then the button change into the "Stop Service" button but when I got out of activity, and then came back in , the S"top Service" button change back into "Start Service" button. same like when the "Service Completed" messages comes the button disappear but later on coming back again to activity , there is a button again.

What I want:

I know that what is the problem and that is "Callback" gets called when the event occurred. But how can I save the state of button to get persistent or if there is something like callback to check that if the service is running and get the current status also ?

Please let me know if there is such a way of checking out ? I hope there would be, Please tell me ans share a little example also. It would be great help.

Edit One:

Also I am facing problem in setting the activity as a result receiver. I know how to set it when it is the same class but the problem is I am starting the service in splash activity and trying to set the main activity to get the results but it is not permitting look here

mReceiver = new DownloadResultReceiver(new Handler()); mReceiver.setReceiver(MainActivity.class); //getting error

and

 mReceiver = new DownloadResultReceiver(new Handler());
            mReceiver.setReceiver(Spalsh.this);// no error

2条回答
Viruses.
2楼-- · 2019-08-11 02:48

You just need to call the method which will check the status of the service in onResume mthod of your activity like this.

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

In onResume

 @Override
        protected void onResume() {
            super.onResume();
       boolean serviceStarted= checkServiceStatus();
    if (serviceStarted) {
        mSvcButton.setText("Service Running");
    } else {
        mSvcButton.setText("Service not Running ... Click to Start");
    }
    }
查看更多
地球回转人心会变
3楼-- · 2019-08-11 03:03

Your problem: but when I got out of activity [onBackPressed();], and then came back in [onCreate()]. This resets everything in your activity to default.

Solution: You can as well set a Flag in your service class and check the flag on the onResume or onCreate method of your activity class. For example:

public class HelloService extends Service {
   public static boolean isRunning = false;
   /** Called when the service is being created. */
   @Override
   public void onCreate() {
       this.isRunning = true;
   }

   /** The service is starting, due to a call to startService() */
   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      return START_STICKY;
   }

   @Override
   public IBinder onBind(Intent intent) {
      return null;
   }

   /** Called when The service is no longer used and is being destroyed */
   /* This method is not guaranteed to be called when android kill this service*/
   @Override
   public void onDestroy() {
       this.isRunning = false;
   }
}

Then in your activity, you can do the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(HelloService.isRunning){ //this will also return false even when android kills your service
        myButton.setText("Stop Service");
    }
    else{
        myButton.setText("Start Service");
    }
}

This approach is faster than iterating through list of running services on android os, In case your user's phone is running lots of services.

As for the BroadcastReceiver, you need to have a class that extends BroadcastReceiver and implement the onReceive method of the class. For example:

public class MyReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        //do work here
    }
}

Now you can set your receiver to the instance of this class:

mReceiver.setReceiver(new MyReceiver());
查看更多
登录 后发表回答