0

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

Ronak Mutha
  • 304
  • 1
  • 2
  • 13
  • Duplicate: http://stackoverflow.com/questions/22257310/android-keep-broadcastreceiver-in-background – alzee Aug 01 '16 at 17:37
  • your phone is Android N? – Lucas Ferraz Aug 01 '16 at 17:49
  • 1
    @M.Ron refer this link http://stackoverflow.com/questions/38422551/gcm-push-notification-not-revived/38423390#38423390 – Hardik Parmar Aug 01 '16 at 17:57
  • Note that if a user "force stop" your application, then everything goes off, including receiving any broadcast (even local), services, activities, etc. As far as application goes, nothing can be done, until your application is "launched" again. – Bonatti Aug 01 '16 at 18:13
  • @Bonatti Does that include swiping from recents? – Kevin Krumwiede Aug 01 '16 at 18:31
  • No. It is from "Config -> Applications -> YOUR_APP -> Force Stop", or any "Application Manager" that the user can have installed (usually on a rooted device), that can programatically stop other applications... Edit your question, and add as much information as you have, such as Logcats, the device model/manufacturer, the Android version, build number, etc. – Bonatti Aug 01 '16 at 18:36
  • Just to make sure we're all clear - "does not work" as in "isn't called" or as in "doesn't do what I expect?" – EJoshuaS - Stand with Ukraine Aug 01 '16 at 20:20
  • @Bonatti I am unable to run by tasks in the broadcast receiver even if I clear my clear my recent applications – Ronak Mutha Aug 01 '16 at 20:28
  • Post your entire AndroidManifest. This is likely a permission issue (EACESS on your LogCat?). Probably you didnt request permissions to observe Network state changes. And again, read the last comment. Post any and all relevant information you may have. These can help us, help you. Logcat, Android version, device manufacturer, etc. – Bonatti Aug 01 '16 at 20:32
  • @Bonatti It works well when I run the app and change my network ,But not able to when the app is not running ---> I am using this as permissions android.permission.INTERNET and android.permission.ACCESS_NETWORK_STATE – Ronak Mutha Aug 01 '16 at 20:37
  • @LucasPaolillo Its Lollipop – Ronak Mutha Aug 01 '16 at 20:43
  • Since you never added the relevant information, we can safely guess that you are not requesting runtime permissions. Set your API level to 19 (Kitkat, before most the security changes started, and run in "legacy mode") and check if the issue continues. And again, POST any and ALL relevant info, into your question. Press the edit button, and writte it down – Bonatti Aug 01 '16 at 20:52
  • I even tried API 19 and above @Bonatti – Ronak Mutha Aug 01 '16 at 21:15

3 Answers3

3

Finally it worked in other android phones. as broadcast receivers and services are by default not allowed in Xiaomi phones

Ronak Mutha
  • 304
  • 1
  • 2
  • 13
1

I would try putting a android:process=":remote" in the Manifest definition of the receiver so that it will run on a separate task.

Ira Juneau
  • 294
  • 1
  • 5
1

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

thepoosh
  • 12,497
  • 15
  • 73
  • 132