简体   繁体   English

当应用未运行时,广播接收器不起作用

[英]Broadcast Receiver not working when app is not running

My broadcast receiver does not work when the application is not running in the background. 当应用程序不在后台运行时,我的广播接收器不起作用。

Tried with different intent filters 尝试了不同的意图过滤器

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.NoActionBar" >

    <receiver android:name=".network.NetworkChangeReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

    <activity
        android:name=".view.activity.MainActivity"
        android:label="@string/app_name">
    </activity>
    <activity android:name=".view.activity.BarLoginActivity"
        android:label="@string/app_name">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

My receiver works fine when the app is running in the background but as soon as I remove my recent apps it does not work. 当应用程序在后台运行时,我的接收器工作正常,但是一旦我删除了最近使用的应用程序,它就无法工作。 Running on API 19 在API 19上运行

Finally it worked in other android phones. 最终,它可以在其他Android手机上使用。 as broadcast receivers and services are by default not allowed in Xiaomi phones 因为默认情况下小米手机不允许广播接收器和服务

我会尝试将android:process =“:remote”放在接收方的清单定义中,以便它将在单独的任务上运行。

Haven't seen your entire AndroidManifest but my first suspect is that you're lacking the proper permission. 尚未看到整个AndroidManifest但我首先怀疑的是您缺少适当的权限。
In order to listen to network changes you must declare that you are using this permission in the manifest like this 为了收听网络更改,您必须在清单中声明您正在使用此权限,如下所示

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

the next suspect is the name you gave, maybe try giving the full path to the reciever name 下一个可疑者是您提供的名称,也许尝试提供收件人名称的完整路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM