简体   繁体   中英

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

Finally it worked in other android phones. 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.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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