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

Alpha-Centauri :

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

Alpha-Centauri :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328116&siteId=1