gpt4 book ai didi

android - LocalBroadcastManager 未按预期工作

转载 作者:行者123 更新时间:2023-11-29 20:58:02 27 4
gpt4 key购买 nike

在我的应用程序中,我必须从 IntentService 类通知我的 Activity 。为此,我正在使用 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() 中没有收到任何东西。请帮忙。提前致谢!!

最佳答案

尝试

 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);


}
}

也在 list 中:

<service android:name="com.commonsware.android.localcast.RegisterAlramIntentService"/>

参见 this

关于android - LocalBroadcastManager 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27031997/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com