从Application Class执行Activity的AsyncTask

时间:2016-07-13 16:48:35

标签: android android-asynctask android-handler

我正在尝试找到一种自动执行AsyncTask的方法,目前它正在按下按钮。

给出一些背景 -

申请类

// The Handler that gets information back from the BluetoothService
public final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {                
            case MESSAGE_READ:
                Log.d(TAG, "MESSAGE_READ");
                byte[] readBuf = (byte[]) msg.obj;
                readMessage = new String(readBuf);
                break;
        }
    }
};

活动 - AsyncTask子类

private class PostTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        readBuf = ((MyApplication) getApplication()).getReadBuf();
        speedcur1 = speedometer.getCurrentSpeed();
        speedcur2 = speedometer1.getCurrentSpeed();
    }

    @Override
    protected Void doInBackground(Void... params) {
        if (readBuf.startsWith("V")) {
            readBuf = readBuf.replace("V", "");
            String[] parts = readBuf.split(",");
            String part1 = parts[0];
            String part2 = parts[1];
            speed1 = Float.parseFloat(part1);
            speed2 = Float.parseFloat(part2);
            finalspeed1 = ((speed1 * 102) / 100);
            finalspeed2 = ((speed2 * 602) / 100);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        speedometer.onSpeedChanged(speedometer.getCurrentSpeed() - speedcur1);
        speedometer.onSpeedChanged(speedometer.getCurrentSpeed() + finalspeed1);
        speedometer1.onSpeedChanged(speedometer1.getCurrentSpeed() - speedcur2);
        speedometer1.onSpeedChanged(speedometer1.getCurrentSpeed() + finalspeed2);
        myLabel.setText(readBuf);
    }
}

我扩展了我的Application类,在这个类中是一个处理程序,它读取从我的服务发送的消息,在这个处理程序中读取msg.arg1并选择适当的情况。

在我的message_read案例中,msg.obj保存为byte [],然后保存为String。

我目前的想法是在检查相关活动当前是否正在运行之后,以某种方式从应用程序类处理程序中执行我的活动中的AsynTask。最初我在循环中使用了这个功能,但是在对我的应用程序进行了大量更改之后,由于两个活动之间共享了蓝牙连接服务,因此需求发生了变化。

2 个答案:

答案 0 :(得分:1)

是的,您可以从应用程序类运行AsyncTask并根据您的逻辑检查是否运行?

    public class BaseJuiceApplication extends Application  {

        public static BaseJuiceApplication instance = null;

        public static Context getInstance() {
            if (null == instance) {
                instance = new BaseJuiceApplication();
            }
            return instance;
        }

        @Override
        public void onCreate() {
            super.onCreate();

            if(getPrefs.getBoolean("MyKKey")){
              // any of your logic 
                 new LongOperation().execute("");
                }

        }

    private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
        }

        @Override
        protected void onPreExecute() {}

    }
  }
}

答案 1 :(得分:0)

1.创建一个界面

2.在活动中实现该接口。

3.然后,将该接口从活动注册到应用程序类

4.之后从应用程序类回调

5.确保从onDestroy或onStop

中的活动取消注册
public class ApplicationClass extends Appliation{

    private static ApplicationClass instance;
    private TestInterface mCallBack;

    public final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {                
            case MESSAGE_READ:
                if(mCallBack!=null){
                  mCallBack.doSomething();
                }
                break;
        }
    }
};


    public void onCreate(){
       instance = this;
    }


    public ApplicationClass getInstance(){
        return instance;
    }

    public void register(TestInterface callBack){
      mCallBack = callBack;
    }

    public void unRegister(){
      mCallBack = null;
    }



}

public interface TestInterface{

    void doSomething();

}

public class MainActivity extends Activity implements TestInterface{

  @Override 
  public void doSomething(){

  }

  @Override 
  public void onStop(){
   ApplicationClass.getInstance().unRegister();
  }

  @Override
  public void onStart(){
   ApplicationClass.getInstance().register(this);
  }

}