【问题标题】:Android Static BroadcastReceiver not working when Application is removed from Recent Tasks从最近的任务中删除应用程序时,Android静态广播接收器不起作用
【发布时间】:2017-12-28 13:40:20
【问题描述】:

我能否知道静态声明的 BroadcastReceiver 是否能够接收广播,即使该应用已从“近期任务”中删除?

我正在尝试收听蓝牙设备连接时广播的事件。

AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="FROM_SERVICE"/>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
</application>

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        Log.i("MyReceiver", intent.getAction());
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

        if ( BluetoothDevice.ACTION_ACL_CONNECTED.equals(action) ) {
            Log.i("MyReceiver", "BT device connected!");
            v.vibrate(500); // Vibrate for 500 milliseconds
        }
    }
}

参考:

Why BroadcastReceiver works even when app is in background ?

Keep broadcast receiver running after application is closed

Android BroadcastReceiver Not being called after app closed from Running App List

【问题讨论】:

  • 不,它不会接收广播,因为当您从最近的任务中删除应用程序时,BR 将被取消注册。
  • @PiyushGupta 那还有其他方法吗?我已经尝试过 Service 和 IntentService,当应用程序从最近的任务中删除时,它们也都被杀死了。我们是否能够做类似的事情,比如从最近的任务中删除音乐应用程序,但它仍会播放音乐并且控件位于通知栏中?谢谢!
  • @Wilson 对此问题有任何解决方案,因为对于某些特定的手机,我也面临同样的问题。
  • 嗨@Parth,我很抱歉,但根据我目前的知识,这是不可能的。但是,根据您的应用程序,您可以将推送通知作为替代方案。

标签: android broadcastreceiver


【解决方案1】:

即使您将其从最近的任务中删除,静态声明的广播也将起作用。 从开发人员站点添加文档。希望这会有所帮助!

清单声明的接收器如果您在 您的清单,系统会启动您的应用程序(如果该应用程序尚未 running) 发送广播时。

注意:如果您的应用面向 API 级别 26 或更高级别,则不能使用 清单以声明隐式广播的接收器(广播 不专门针对您的应用程序),除了一些隐含的 不受该限制的广播。在大多数情况下,您 可以改为使用计划作业。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多