简体   繁体   English

如何使用第二个线程中的“startActivity”将 Intent 传递给主线程?

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

I want to use an Intent with start(Intent) and do different things controled by a Thread.我想使用带有 start(Intent) 的 Intent 并执行由线程控制的不同事情。 I know I need the main Context to use "startActivity" but how do I manage this from a seperate Thread?我知道我需要主上下文来使用“startActivity”,但是如何从单独的线程管理它? is it possible to link the "startActivity" to the main Thread using a Handler or something?是否可以使用处理程序或其他方式将“startActivity”链接到主线程?

Example use:使用示例:

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

Update更新

This is the Code I have problems with:这是我有问题的代码:

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主要活动

This is what I made in the 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);
            }
        });

    }
}

Question

How can I execute some Intents from a secound thread written in a seperated class?如何从单独的类中编写的第二个线程执行一些 Intent?

If this idea itself is not right:如果这个想法本身不对:

I don't know how to pass an Intent to the main Thread using "starActivity".我不知道如何使用“starActivity”将 Intent 传递给主线程。

Activity.runOnUiThread(Runnable)

is the method you need.是你需要的方法。 It posts a runnable (in this case one that starts an activity) to the main Android thread.它向主 Android 线程发布一个可运行的(在本例中是启动活动的)。

Can be done as follows:可以按如下方式进行:

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

You need for link to context, activity or any view on activity.您需要链接到上下文、活动或任何活动视图。 Make runnable of code, that should be executed in main thread使代码可运行,应在主线程中执行

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

For context:对于上下文:

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

for activity:活动:

activity.runOnUiThread(your_code)

for view:查看:

view.post(your_code)

All you need is Context object which can be called from any thread您只需要可以从任何线程调用的 Context 对象

If you are storing Context instance, please make sure to hold as ApplicationContext() to avoid memory leak如果您正在存储 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();

Solution is WeakReference<>解决方案是 WeakReference<>

In the new Thread, you need to implement the following:在新的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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM