【问题标题】:Activity doesn't receive Broadcast from IntentServiceActivity 没有收到来自 IntentService 的广播
【发布时间】:2013-12-19 06:59:04
【问题描述】:

我正在尝试从 IntentService 向 Activity 发送广播,但它不起作用,即使服务确实发送了广播(我通过调试器工具检查)。

奇怪的是,我几乎没有其他广播服务,但只有这个特定的服务不起作用。

这是我的代码:

意图服务:

    protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Intent myItent = new Intent ("test");
    sendBroadcast(intent);
}

MainActivity 中的广播接收器:

    private BroadcastReceiver testbcreceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(getApplicationContext(), "succeed",
                Toast.LENGTH_SHORT).show();
        System.out.println("success");
    }

};

onResume,我在这里注册 BroadcastReceiver。请注意,我这里有 4 项服务,4 项中有 2 项工作正常。

    protected void onResume() {
    super.onResume();
    mds.open();
    registerReceiver(testbcreceiver, new IntentFilter("test"));

    registerReceiver(downloadServiceReceiver, new IntentFilter(
            DownloadChapterService.NOTIFICATION));

    registerReceiver(parsingMangaReceiver, new IntentFilter(
            ParsingMangaLinkService.NOTIFICATION));

    registerReceiver(parsingMangaChapterReceiver, new IntentFilter(
            ParsingChapterMangaService.NOTIFICATION));
}

在 AndroidManifest.xml 中:

<service android:name="anvu.bk.service.ToastService">
    </service>

感谢您查看我的问题。

【问题讨论】:

  • 您是否在 Activity 上添加了 onReceive 方法,是否这样做:public class ResponseReceiver extends BroadcastReceiver { public static final String ACTION_RESP = "mypackagename.intent.action.MESSAGE_PROCESSED"; @Override public void onReceive(Context context, Intent intent) { // TODO 如果消息指示成功发布到服务器,则开始对话} }
  • 我相信我已经在问题中发布了那部分代码。请检查它:)。
  • 好吧,我解决了这个问题。结果我忘记更新意图过滤器的新“动作”名称。谢谢大家。 :D

标签: android broadcastreceiver


【解决方案1】:

改变它

protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Intent myItent = new Intent ("test");

    sendBroadcast(intent);
    System.out.println("wait");

}

protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Intent myItent = new Intent ();
    myItent .setAction(DownloadChapterService.NOTIFICATION); // Define intent-filter
    sendBroadcast(myItent );
    System.out.println("wait");

}

【讨论】:

  • 糟糕,这不是原始代码。谢谢你指出这一点。问题仍然是未解决的思想。 *编辑:我刚刚编辑了问题。
【解决方案2】:

在创建要广播的意图时,在您的操作前加上您的包名称并将其设置如下:

public static final String TEST_ACTION = "anvu.bk.service.TEST_ACTION";

protected void onHandleIntent(Intent intent) {
    Intent myItent = new Intent ();
    myIntent.setAction(TEST_ACTION);
    sendBroadcast(intent);
}

然后,在你的 onResume() 中,注册你的接收者:

IntentFilter filter = new IntentFilter();

//basically, we need the same string as when we were preparing intent for broadcast
//so set action this way, or use string "anvu.bk.service.TEST_ACTION" instead
//of course, use the class name where you declared TEST_ACTION :)
filter.addAction(IntentService.TEST_ACTION); 

registerReceiver(testbreceiver, filter);

然后记得在 onDestroy() 中取消注册您的接收器:

unregisterReceiver(testbreceiver);

附带说明,不要使用 System.out.println() - 使用 Android 的 Log.d() 来记录内容。 Here's why:

不应使用 System.out.println()(或原生代码的 printf())。 System.out 和 System.err 被重定向到 /dev/null,因此您的打印语句将没有可见的效果。但是,为这些调用发生的所有字符串构建仍然会执行。

【讨论】:

  • 原来我决定在某一时刻更改action的名称,但忘记在Activity中更新它。谢谢!你确实帮我解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2018-07-30
相关资源
最近更新 更多