简体   繁体   中英

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. I know I need the main Context to use "startActivity" but how do I manage this from a seperate Thread? is it possible to link the "startActivity" to the main Thread using a Handler or something?

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

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?

If this idea itself is not right:

I don't know how to pass an Intent to the main Thread using "starActivity".

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.

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

If you are storing Context instance, please make sure to hold as ApplicationContext() to avoid memory leak

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

In the new Thread, you need to implement the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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