我有一个服务类,通过它可以将一些数据发送到我的活动
public class baseApi extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//get the receiver object
resultReceiver = intent.getParcelableExtra("receiver");
}
private BroadcastReceiver datachanged_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//HERE I send some data like this
Bundle bundle = new Bundle();
bundle.putString("data", "My data goes here");
resultReceiver.send(100, bundle);
}};
}
在同一个类中,我有一个扩展“thread”的类,这个类也打算使用resultReceiver向UI发送一些数据,它不会给出任何异常,但是在UI端也没有收到数据,而结果通过广播接收器发送数据的结果很好。
public class baseApi extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//get the receiver object
resultReceiver = intent.getParcelableExtra("receiver");
}
private BroadcastReceiver datachanged_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//HERE I send some data like this
Bundle bundle = new Bundle();
bundle.putString("data", "My data goes here");
resultReceiver.send(100, bundle);
}};
class actionThread extends Thread{
@Override
public void run() {
super.run();
try{
//send this data to the service
Bundle bundle = new Bundle();
bundle.putString("data","More data goes here" );
resultReceiver.send(200, bundle);
}
catch(Exception ex)
{
}
}
}
}
答案 0 :(得分:1)
在android中,您可以使用Handler
与UI线程进行通信。只需使用Hanlder.Post
或HandlerThread
代替。