【问题标题】:Broadcast receiver is working on emulator and phone, but not tablet广播接收器正在模拟器和手机上工作,但不是平板电脑
【发布时间】:2014-12-16 20:38:52
【问题描述】:

我的 android 应用程序包含多次启动以在树状结构中移动的活动。为了关闭这个结构的所有活动,我声明了一个广播接收器来等待意图并在需要时完成活动。此活动的每个实例都使用相同的操作名称启动自己的接收器。

这个应用程序的所有方面都在模拟器 API20 和 4.1.2 版本的索尼 Xperia S 上运行。 在 Manta MID709 平板电脑(与 xperia 相同的 android 版本)上,接收器仅在特定活动位于顶部(正在运行且可见)时工作。因此,如果我启动新内容,它会关闭此活动的一个实例,或者不关闭。

声明广播接收器的代码(在onCreate之外,但在Activity内部):

public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.application.STOP")) {
            unregisterReceiver(broadcastReceiver);
            Toast.makeText(getApplicationContext(), "Intent Received", Toast.LENGTH_LONG).show();
            finish();

        }
    }
};

激活关闭的代码:

Intent intent2 = new Intent();
        intent2.setAction("com.application.STOP");
        sendBroadcast(intent2);

在模拟器和 xperia 上,我以三个(三个实例正在运行)“Intent Received”祝酒词结束,然后应用程序关闭。在平板电脑上,有时会有一个“Intent received” toast 或没有。

【问题讨论】:

    标签: android broadcastreceiver multiple-instances


    【解决方案1】:

    答案很简单,代码 100% 好。我已经在开发人员选项中签入了我的平板电脑以不保留活动。换句话说,中止所有不存在的活动。它正在杀死我的广播接收器。希望这个答案会对某人有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多