我的服务必须通过BradcastReceiver与Activity(MyActivity)通信。这个BroadcastReceiver声明了intosame活动,所以:
private class MessaggiReceiver extends BroadcastReceiver {
public static final String ACTION_RELOAD = "it.android.dev.thecode.ACTION_RELOAD";
@Override
public void onReceive(Context arg0, Intent intent) {
if(intent.getAction().equals(ACTION_RELOAD)){
leggi_act; //Method of MyActivity
}
}
}
但是如何在Manifest中声明MyActivity中包含的brodcast?我在it.android.dev.thecode.MyActivity.MessaggiReceiver中使用了这段代码
<receiver android:name="it.android.dev.thecode.MyActivity.MessaggiReceiver">
<intent-filter>
<action android:name="it.android.dev.thecode.ACTION_RELOAD"></action>
</intent-filter>
</receiver>
但是错了,然后我用it.android.dev.thecode.myactivity $ MessaggiReceiver
使用了这段代码。<receiver android:name="it.android.dev.thecode.MyActivity$MessaggiReceiver">
<intent-filter>
<action android:name="it.android.dev.thecode.ACTION_RELOAD"></action>
</intent-filter>
</receiver>
由于
答案 0 :(得分:1)
您无需在清单中注册广播接收器,而是通过活动的onResume中的代码进行注册。不要忘记在onPause中取消注册以避免任何泄漏。
@Override
public void onResume() {
registerReceiver(mMessageReceiver,
new IntentFilter("it.android.dev.thecode.ACTION_RELOAD"));
}
@Override
public void onPause() {
unregisterReceiver(mMessageReceiver);
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("receiver", "Got broadcast: " + intent.getAction());
}
};