在我的应用程序中,我必须从IntentService类中通知我的活动。为此,我正在使用LocalBroadcastManager。但是我在我的广播接收器的onReceive中没有收到任何东西。这是我写的。
在我的BaseActivity中,我注册了我的接收器。
public class BaseActivity extends FragmentActivity implements App {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter(custom-event-name));
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
System.out.println("Overlay Message" +bundle.getString("message"));
}
};
}我正在从我的RegisterAlarmIntentService课堂上发送一个本地广播。
public class RegisterAlramIntentService extends WakefulIntentService implements
APIConstants {
public RegisterAlramIntentService() {
super("AlarmService");
}
@Override
public String getTag() {
return "AlarmService";
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("Working till here fine In RegisterAlarm");
Bundle bundle = intent.getExtras();
Intent localIntent = new Intent(custom-event-name);
localIntent.putExtras(bundle );
LocalBroadcastManager.getInstance(this).sendBroadcast(
localIntent);
}
}调用onHandleIntent()方法。但是,我的接收器的onReceive()中没有接收到任何信息。请帮帮忙。提前谢谢!!
发布于 2014-11-20 06:00:15
试一试
public class RegisterAlramIntentService extends WakefulIntentService implements
APIConstants {
Intent localIntent;
public RegisterAlramIntentService() {
super("AlarmService");
localIntent = new Intent(custom-event-name);
}
@Override
public String getTag() {
return "AlarmService";
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("Working till here fine In RegisterAlarm");
Bundle bundle = intent.getExtras();
Thread.sleep(5000); // For Testing only because it is in whole new thread (which may not wait for your reciever to setup)
localIntent.putExtras(bundle );
LocalBroadcastManager.getInstance(this).sendBroadcast(
localIntent);
}
}也在清单中:
<service android:name="com.commonsware.android.localcast.RegisterAlramIntentService"/>请参阅this
https://stackoverflow.com/questions/27031997
复制相似问题