简体   繁体   English

将数据从线程传递到活动

[英]Pass data from thread to activity

In my application I have an activity that starts a thread 在我的应用程序中,我有一个启动线程的活动

connectThread = new ConnectThread(MainActivity.this , device);
connectThread.start();

in this thread 2 things are done : 1. a new thread is created 2. and a new activity is started 在此线程中,完成了2件事:1.创建了一个新线程2.并开始了一个新的活动

connectedThread = new ConnectedThread(mmSocket);
connectedThread.start();

    handler.post(new Runnable() {

        @Override
        public void run() {
            MainActivity.connectingProgressBar.setVisibility(View.GONE);

            Intent startPostGet = new Intent(context, PostGetActivity.class);
            startPostGet.putExtra(MainActivity.EXTRA_DEVICE, mmDevice);
            context.startActivity(startPostGet);                
        }
    });

in the latest created thread (connectedthread) I receive data and want to pass this to the newest activity (postgetactivity) 在最新创建的线程(connectedthread)中,我接收数据并将其传递给最新的活动(postgetactivity)

i was thinking about doing something like this in the connectedthread: 我正在考虑在connectedthread中执行以下操作:

handler.post(new Runnable() {
        @Override
        public void run() {
            PostGetActivity.statusTextView.setText("Got data");
            PostGetActivity.processData(data)
        }
    });

The problem is that the function processData() may not be static but I am referencing it from a static context. 问题在于函数processData()可能不是静态的,但我是从静态上下文中引用它的。

How can I fix this? 我怎样才能解决这个问题? Any help will be appreciated. 任何帮助将不胜感激。

You can force your handler to post to the main thread as so: 您可以这样强制处理程序将其发布到主线程:

Handler mainHandler = new Handler(getMainLooper());

All UI operations need to be on the main thread, so since you are calling them from a thread you created you need to do that. 所有UI操作都必须在主线程上,因此,由于您是从创建的线程中调用它们的,因此需要这样做。

Secondly, you need to pass data to the new activity in the Intent object as you have done earlier. 其次,您需要像之前所做的那样将数据传递到Intent对象中的新活动。

Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("KEY", "String data");
startActivity(intent);

The main thing is, your handler should post the runnable to the main thread (As shown above). 最主要的是,您的处理程序应将可运行对象发布到主线程中(如上所示)。

Do this in connectedthread . connectedthread执行此操作。 Using FLAG_ACTIVITY_SINGLE_TOP won't restart your activity but a specific callback onNewIntent will be called 使用FLAG_ACTIVITY_SINGLE_TOP不会重新启动您的活动,但是将调用onNewIntent的特定回调

Intent i = new Intent(context, PostGetActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("data", "data_from_thread");
context.startActivity(i);

You can get data in PostGetActivity like this 您可以像这样在PostGetActivity获取数据

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String data = intent.getStringExtra("data"); // you will get "data_from_thread"
    // use this string the way you want
}

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

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