【问题标题】:How can I pass an Intent to the main thread using "startActivity" from a secound thread?如何使用第二个线程的“startActivity”将 Intent 传递给主线程?
【发布时间】:2019-10-09 03:24:59
【问题描述】:

我想使用带有 start(Intent) 的 Intent 并执行由 Thread 控制的不同事情。 我知道我需要主上下文才能使用“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 中所做的

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 传递给主线程。

【问题讨论】:

标签: java android multithreading start-activity


【解决方案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)

【讨论】:

  • 喜欢:Public Classic mThread extends MainActivity implements Runnable{ @override Public void Run(){ If (true) { Intent intent = new Intent (Bluetoothadapter.Enable()): Activity.runOnUiThread(Intent)
  • 你的例子毫无意义。它至少在 and 中没有两个右大括号,并且没有任何意义来扩展 MainActivity。修改您的示例并格式化您的代码
  • activity.runOnUiThread(your_code) 给我:不能从静态上下文引用非静态方法 'runOnUiThread(java.lang.Runnable)'
  • 它会导致它的非静态方法。您不能编写 Activity.runOnUIThread,您必须创建私有 Activity mACtivity = link_to_my_activity 之类的字段。现在您可以使用 mActivity.runInUIThread。您必须在子线程中提供 link_to_my_activity,这是创建另一个问题所需的方法
【解决方案2】:
Activity.runOnUiThread(Runnable)

是您需要的方法。它会向 Android 主线程发布一个可运行的(在本例中是启动 Activity)。

可以按如下方式进行:

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

【讨论】:

  • 赞:Public Classic mThread extends MainActivity implements Runnable{
  • 我不知道你的代码是什么样的,但我会详细说明一下。
  • 处理程序不应定义为静态:public Handler mainHandler = ...
  • 不,它只是不起作用,应用程序在到达这一点时崩溃。
  • 使用调试器,找出应用崩溃的错误。
【解决方案3】:

你只需要一个可以从任何线程调用的 Context 对象

如果你存储的是 Context 实例,请确保保存为 ApplicationContext() 以避免内存泄漏

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

new Thread(new Runnable(){

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

【讨论】:

    【解决方案4】:

    解决方案是弱引用

    在新的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)
           }
    }
    

    MainActivity onCreat

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

    【讨论】:

      【解决方案5】:
      new Thread(new Runnable(){
      public void run(){
      getApplicationContext.startActivity(new Intent(getApplicationContext,MainActivity.class));
       }
      }).start();
      

      【讨论】:

        猜你喜欢
        • 2022-01-14
        • 1970-01-01
        • 2019-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 2021-08-15
        • 1970-01-01
        相关资源
        最近更新 更多