如何通过线程更新非MainActivity活动?

时间:2019-06-19 07:17:13

标签: android multithreading android-activity

我要更新不是MainActivity的活动。

因此,我通过MainActivity中的onClick方法启动了第二个Activity。

现在,“ SecondActivity”活动位于最前面。 当我在“ MainActivity”中启动线程时,如何引用“ SecondActivity”来更新其TextView等?

伪代码

 public class activity_MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ThreadSome threadSome= new ThreadSome();
        threadSome.start()
    }

   onClick(View View){

   Intent intent = new Intent(this, activity_Second.class);
   startActivity(intent);
   }
}

内部线程


public class ThreadSome extends Thread {

    @Override
    public void run() {
      //This is what I don't know, so I just write what I want to do.
      // I know the following Code is wrong and not working.

      activity_Second.someTextView.setText("Hi");
    }
}

WeakReference是执行此操作的最佳方法,还是更好地与静态TextView对象一起使用?您将如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

根据您的描述,我认为您想做一些事情,以便基于在forground活动中执行的某些事件在活动堆栈中进行一些ui更改。有一个很好的方法可以通过startActivityForResult()使用onActivityResult(),但是如果这不能直接满足您的要求,那么您可以尝试以下方法:

/**
    UpdateActivity is the activity where some ui update or action will be taken based on event in EventActivity.
**/

public class UpdateActivity extends Activity {

    private BroadcastReceiver mReceiver;
    public static final String ACTION_UPDATE = "com.my.internal.activity.action";

    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        ......

        //Prepared Intent for broadcast receiver
        IntentFilter intentFilter = new IntentFilter(ACTION_UPDATE);
        //registering our receiver
        this.registerReceiver(mReceiver, intentFilter);

        .....
    }

    //This is the receiver section where you need to do the ui update
    mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //extract our message from intent
            String some_msg = intent.getStringExtra("msg_1"); //parameter received if passed in intent when broadcast called.
            //log our message value
            Log.i("Message", some_msg);

            updateActivityUi();
        }
    };

    private void updateActivityUi() {
        // you need to write the code for the update which you want to do after an event done in other activity.
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //unregister our receiver
        this.unregisterReceiver(this.mReceiver);
    }
}

public class EventActivity extends Activity {

    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);

        ......

        //Sending BroadcastReceiver against the action ACTION_UPDATE and it will be received by UpdateActivity.
        if(condition_for_event) {
            Intent i = new Intent(UpdateActivity.ACTION_UPDATE).putExtra("msg_1", "Hey! an event performed here.");
            this.sendBroadcast(i);
        }

        .....
    }

    ....
}

让我知道它是否解决了您的问题。