2

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

Community
  • 1
  • 1
Alpha-Centauri
  • 316
  • 2
  • 13
  • 1
    Possible duplicate of [What exactly does the post method do?](https://stackoverflow.com/questions/13840007/what-exactly-does-the-post-method-do) – TheChubbyPanda May 22 '19 at 16:19

5 Answers5

1
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(...);
});
TheChubbyPanda
  • 1,721
  • 2
  • 16
  • 37
1

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)
  • Like: Public Classic mThread extends MainActivity implements Runnable{ @override Public void Run(){ If (true) { Intent intent = new Intent (Bluetoothadapter.Enable()): Activity.runOnUiThread(Intent) – Alpha-Centauri May 22 '19 at 15:48
  • Your exaple meaningless. It has no two closing braces in the and at least, and it have no sence to make smth extend MainActivity. Modify your example and format you code – Спицко Дмитрий May 23 '19 at 07:08
  • activity.runOnUiThread(your_code) gives me: Non-static method 'runOnUiThread(java.lang.Runnable)' cannot be referenced from a static context – Alpha-Centauri May 23 '19 at 12:29
  • it cause its non static metod. You cant write Activity.runOnUIThread, you have to create field like private Activity mACtivity = link_to_my_activity. And now you can use mActivity.runInUIThread. You have to give link_to_my_activity in your subthread, the ways to do it need for creating another question – Спицко Дмитрий May 24 '19 at 06:06
1

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();
0
new Thread(new Runnable(){
public void run(){
getApplicationContext.startActivity(new Intent(getApplicationContext,MainActivity.class));
 }
}).start();
0

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();
Alpha-Centauri
  • 316
  • 2
  • 13