2016-07-13 19 views
0

我試圖找到一種方法來自動執行AsyncTask,目前它的工作方式是按下按鈕。從應用程序類執行活動的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); 
    } 
} 

我已經延長我的應用程序類,這個類裏面的處理程序它會讀取我的服務發送的消息在此處理程序中讀取並選擇適當的大小寫。

在我的message_read情況下,msg.obj保存爲byte [],然後保存爲String。

我目前的想法是在檢查是否有問題的活動正在運行後,以某種方式在應用程序類處理程序中執行我的活動中的AsynTask。最初我在循環內部使用了該功能,但是在對應用程序進行了大量更改之後,由於兩個活動之間共享藍牙連接服務,所以需求發生了變化。

+0

倒不如放一點你的代碼,以便我們可以幫你 –

+0

做過,留下了一些不相關的代碼,尤其是在處理程序 – sab215

回答

0

1.創建界面

2.在活動中實現接口。

3.Then,從註冊的活動,接口爲應用程序類

4.After比從做應用程序類回調

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

} 
+0

可不可以給的這個簡短的代碼示例,請? – sab215

+0

@ sab215:添加 –

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() {} 

    } 
    } 
} 
+0

AsyncTask現在在我的活動中,而不是應用程序類,雖然它可能是另一種選擇,但我必須找到一種方法來從Application類的AsyncTask子類更新我的活動UI。 – sab215

+0

你可以從更新您的AsyncTask只是用戶界面UI更新將在'mContext.runOnUiThread封裝(新的Runnable(){ 公共無效的run(){// 請在UiThread } 東西});' –

相關問題