如何使用来自secound线程的“ startActivity”将Intent传递给主线程?

时间:2019-05-22 15:24:20

标签: java android multithreading start-activity

我想将Intent与start(Intent)一起使用,并执行由线程控制的其他操作。 我知道我需要主上下文才能使用“ startActivity”,但是如何从单独的线程进行管理? 是否可以使用Handler或其他方法将“ 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

这就是我在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传递给主线程。

5 个答案:

答案 0 :(得分:1)

Activity.runOnUiThread(Runnable)

是您需要的方法。它将可运行的内容(在本例中为启动活动的内容)发布到Android主线程。

可以执行以下操作:

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

答案 1 :(得分:1)

您需要链接到上下文,活动或活动的任何视图。使代码可运行,应在主线程中执行

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)

答案 2 :(得分:1)

您需要的是可以从任何线程调用的Context对象

如果要存储Context实例,请确保将其保留为ApplicationContext()以避免内存泄漏

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

new Thread(new Runnable(){

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

答案 3 :(得分:0)

新线程(新Runnable(){ public void run(){ getApplicationContext.startActivity(new Intent(getApplicationContext,MainActivity.class));  } })。start();

答案 4 :(得分:0)

解决方案是弱引用<>

在新线程中,您需要实现以下内容:

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

MainActivity onCreat

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