我有一个服务类,我通过它向我的活动发送一些数据
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);
}};
}
在同一个类中,我有一个扩展“线程”的类,并且该类还打算使用 resultReceiver 向 UI 发送一些数据,它没有给出任何异常,但在 UI 端也没有收到数据,而 resultReceiver通过广播接收器发送数据效果很好。
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)
{
}
}
}
}