在服务中执行了某些操作后,我正在尝试更新 UI(活动)。这是一个非常简单的例子,但它似乎对我不起作用。我在这里想念什么?
示例服务:
public class ExampleService extends IntentService{
@Override
protected void onHandleIntent(Intent intent) {
notifyActivity();
}
private void notifyActivity() {
Intent broadcast = new Intent(this, ExampleActivity.class);
sendBroadcast(broadcast);
}
}
示例活动:
public class ExampleActivity extends ListActivity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
@Override
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
registerReceiver(receiver, filter);
}
}