2

I'm trying to listen for changes in network connectivity. My broadcast receiver doesn't seem to be firing when I switch in or out of airplane mode. I'm not sure if this is because I don't have it registered correctly or if maybe airplane mode changes don't cause this to fire. Any advice is appreciated. Thanks!

My manifest:

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

<receiver android:name=".NetworkReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

My broadcast receiver class:

public class NetworkReceiver extends BroadcastReceiver {

    private SharedPreferences prefs;
    private SharedPreferences.Editor prefEditor;

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

        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefEditor = prefs.edit();

        Boolean has_connection = prefs.getBoolean("has_connection", false);
        prefEditor.putBoolean("has_connection", !has_connection);
        prefEditor.commit();

        System.err.println("Network Receiver notified of a connectivity change!");
        System.err.println("New has_connection: " + !has_connection);

    }

}

Also, after the app runs for about 45 seconds, it suddenly crashes with the following error:

09-06 21:29:48.775: E/AndroidRuntime(15613): java.lang.RuntimeException: Unable to instantiate receiver com.mysite.myapp.NetworkReceiver: java.lang.ClassNotFoundException: com.mysite.myapp.NetworkReceiver
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • Please refer to this answer for pre and post lollipop devices.[Very Useful for connectivity detection](https://stackoverflow.com/a/37404841/2581109) – Deepak Gupta May 27 '17 at 07:49

3 Answers3

2

This looks like a thread about a similar issue to what you're having. Maybe one of these answers will help:

Unable to instantiate receiver in BroadcastReceiver SMS

Community
  • 1
  • 1
  • Thanks for this. The problem was that my receiver is in a package called 'database' which is in the 'src' folder. Thus, my manifest needed to be `` Now it is receiving connectivity changes and no more crashes. Thanks! – AndroidDev Sep 07 '13 at 04:28
0

have u add permission in the AndroidManifest.xml,

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
Dixit Patel
  • 3,190
  • 1
  • 24
  • 36
  • Yes. I should have posted that. I'll edit my post to show that. I'm also getting an error message after the system fully boots that crashes it. I'll also post this. Thank you! – AndroidDev Sep 07 '13 at 03:30
0

Don't give relative names for receiver. Give full package name as well. For example,

<receiver android:name="com.YouPackageName.NetworkReceiver" >
<intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>

This should solve your problem for sure!

kapil thadani
  • 1,425
  • 14
  • 26
  • Thanks. I've tried that a couple of different ways, but same result. I have this class inside of a package called 'database'. So I've tried `com.mysite.myapp.NetworkReceiver` and also `com.mysite.myapp.database.NetworkReceiver`. Unfortunately, neither seems to solve the issue. In fact, I get the exact same error under all three settings. – AndroidDev Sep 07 '13 at 04:19