Service
我想将带有指定组件的显式意图发送到Activity
. 我已经BroadcastReceiver
在MainActivity.java
.
当我不为 设置组件时Intent
,它会在活动中接收。但是对于指定的组件,它就不起作用了。为什么?
广播服务.java:
public class BroadcastService extends IntentService {
@Override
public void onCreate() {
super.onCreate();
intent = new Intent();
ComponentName component = new ComponentName(
MainActivity.class.getPackage().getName(),
MainActivity.class.getName());
intent.setComponent(component);
}
@Override
protected void onHandleIntent(Intent intent) {
intent.putExtra("time", new Date().toLocaleString());
sendBroadcast(intent);
}
}
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
}