簡體   English   中英

如何使用第二個線程中的“startActivity”將 Intent 傳遞給主線程?

[英]How can I pass an Intent to the main thread using "startActivity" from a secound thread?

我想使用帶有 start(Intent) 的 Intent 並執行由線程控制的不同事情。 我知道我需要主上下文來使用“startActivity”,但是如何從單獨的線程管理它? 是否可以使用處理程序或其他方式將“startActivity”鏈接到主線程?

使用示例:

Public Classic mThread implements Runnable{ 
@override
 Public void Run()
{ 
   If (true) //something is true
   { 
      Intent intent = new Intent (Bluetoothadapter.Enable()):
      startActivity(Intent):
   }
   If(true) //Something else is true
   {
      Intent intent = new Intent (MainActivity, esp.class);
      startActivity (intent)
   }
} 
}

更新

這是我有問題的代碼:

public class cBT_startcheck extends MainActivity implements Runnable {

    private int iCurrent_BT_state = mBluetoothAdapter.getState();
    @Override
    public void run() {
        if (mBluetoothAdapter == null) {
            cGlobal_values.bBT_NOADAPTER =true;

        }else if (mBluetoothAdapter != null)
        {
            cGlobal_values.bBT_NOADAPTER =false;
            switch (iCurrent_BT_state)
            {
                case BluetoothAdapter.STATE_ON:
                    cGlobal_values.bBT_GenState_on=true;
                    break;

                case BluetoothAdapter.STATE_OFF:
                    cGlobal_values.bBT_GenState_on =false;
                    break;
            }
            if (!mBluetoothAdapter.isEnabled()){
                Log.d("HALLO", "run: ");
                //Intent intBT_start = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                //mainContext.startActivity(intBT_start);
                vStart_BT();
            }
        }

    }
}

主要活動

這是我在 MainActivity 中所做的

public class MainActivity extends AppCompatActivity {
   public Handler mainHandler = new Handler(Looper.getMainLooper());
    public void vStart_BT()
    {
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                Intent intBT_start = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivity(intBT_start);
            }
        });

    }
}

如何從單獨的類中編寫的第二個線程執行一些 Intent?

如果這個想法本身不對:

我不知道如何使用“starActivity”將 Intent 傳遞給主線程。

Activity.runOnUiThread(Runnable)

是你需要的方法。 它向主 Android 線程發布一個可運行的(在本例中是啟動活動的)。

可以按如下方式進行:

currentActivity.runOnUiThread(new Runnable {
    startActivity(...);
});

您需要鏈接到上下文、活動或任何活動視圖。 使代碼可運行,應在主線程中執行

Runnable your_code = new Runnable({
    @Override
    public void run(){
         Intent intent = new Intent(context, MyActivity.class);
         startActivity(intent);
    }
};

對於上下文:

Looper looper = context.getMainLooper(); //Looper of main thread
Handler handler = new Handler(looper);
handler.post(your_code); //send your code to executing in main thread

活動:

activity.runOnUiThread(your_code)

查看:

view.post(your_code)

您只需要可以從任何線程調用的 Context 對象

如果您正在存儲 Context 實例,請確保保留為 ApplicationContext() 以避免內存泄漏

final Context ctx = MainActivity.this.getApplicationContext();

new Thread(new Runnable(){

public void run(){
ctx.startActivity(new Intent(ctx,MainActivity.class));
 }
}).start();
new Thread(new Runnable(){
public void run(){
getApplicationContext.startActivity(new Intent(getApplicationContext,MainActivity.class));
 }
}).start();

解決方案是 WeakReference<>

在新的Thread中,需要實現以下內容:

public class cBT_startcheck extends MainActivity implements Runnable {

   private WeakReference<MainActivity> activityWeakReference;

   cBT_startcheck(MainActivity activity){
       activityWeakReference =new WeakReference<MainActivity>(activity);
   }
   @Override
   public void run() {
       final MainActivity activity =activityWeakReference.get();
       Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       activity.startAplication(intent)
       }
}

主要活動在創建

Runnable rcBT_startcheck = new cBT_startcheck(this);
new Thread(rcBT_startcheck).start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM