获取进程的当前状态/服务随时随地活动开始 [英] Getting the current status of process/service anytime the activity starts

查看:229
本文介绍了获取进程的当前状态/服务随时随地活动开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做的意图服务及其所有工作的罚款。我所做的接口,并提出回调。什么叫回做,收到3以下消息:


  1. 服务启动:
    当这个消息到达后,我的启动服务按钮变为停止服务按钮。

  2. 服务完成:
    此消息到达时,我Button获取消失

  3. 错误:
    当这个消息到达我的按钮换上了错误按钮。

的回调是正常使用。测试时我现在已经存在以下问题的注意。这些都为在

问题:

当我点击启动服务按钮,该按钮改变为停止服务按钮启动该服务,但是当我走出的活动,然后在回来的时候,S顶级服务按钮变化回到启动服务按钮。同样,当服务完成的消息而来的按钮消失,但后来再次回来的活动,有又是一个按钮等。

我要什么:

我知道是什么问题,那就是回调事件发生时被调用。但我怎么能保存按钮的状态来获得持续的或有类似的回调来检查,如果该服务正在运行并获得当前的状态也?

请让我知道,如果有检查出了这样的方式?我希望会是这样,请告诉我,答也共享一个小例子。这将是很大的帮助。

修改一:

我也是面临的问题在设置的活动,结果接收器。我知道如何设置它时,它是同一类,但问题是我开始在飞溅的活动服务,并试图设置的主要活动,以获得满意的结果,但它不会在这里允许看


  

mReceiver =新DownloadResultReceiver(新处理程序());
              mReceiver.setReceiver(MainActivity.class); //得到错误



  mReceiver =新DownloadResultReceiver(新的处理程序());
            mReceiver.setReceiver(Spalsh.this); //没有错误



解决方案

您的问题:
但是当我走出活动[onBack pressed();],然后回来了[的onCreate()]。这在您的活动为默认重​​置一切。

解决方案:
您可以同时设置标志在你的服务类,并检查标志上onResume或活动类的onCreate方法。例如:

 公共类HelloService的延伸服务{
   公共静态布尔isRunning = FALSE;
   / **正在创建的服务时调用。 * /
   @覆盖
   公共无效的onCreate(){
       this.isRunning = TRUE;
   }   / **该服务正在启动,由于对startService()的调用* /
   @覆盖
   公众诠释onStartCommand(意向意图,诠释标志诠释startId){
      返回START_STICKY;
   }   @覆盖
   公众的IBinder onBind(意向意图){
      返回null;
   }   / **当服务不再使用,被销毁调用* /
   / *这个方法不能保证被调用时,机器人杀死这个服务* /
   @覆盖
   公共无效的onDestroy(){
       this.isRunning = FALSE;
   }
}

然后在你的活动中,你可以做到以下几点:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    如果(HelloService.isRunning){//这也将返回false,即使机器人杀死你服务
        myButton.setText(停止服务);
    }
    其他{
        myButton.setText(启动服务);
    }
}

这方法比通过Android操作系统运行的服务,如果你的用户的手机运行的是大量的服务列表迭代要快。

对于广播接收器,你需要有一个扩展的BroadcastReceiver类和实现类的方法的onReceive。例如:

 公共类MyReceiver扩展广播接收器{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        //不要在这里工作
    }
}

现在您可以将接收器设置为此类的实例:

  mReceiver.setReceiver(新MyReceiver());

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

解决方案

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

这篇关于获取进程的当前状态/服务随时随地活动开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆