5

When I create class that extends BrodcastReceiver and register it. Error Unable to destroy activity, Receiver not registered: null onDestroy() function in MainActivity When call unregisterReceiver.

onCreate():

ConnectionChangeReceiver connectionChangeReceiver = new ConnectionChangeReceiver();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(connectionChangeReceiver, filter);

onDestroy():

@Override
protected void onDestroy() {
    Log.v("YourActivity", "onDestroy");
    unregisterReceiver(connectionChangeReceiver);
    super.onDestroy();
}

ConnectionChangeReceiver Class:

public class ConnectionChangeReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive( Context context, Intent intent )
    {
        Toast.makeText(context, "CONN", Toast.LENGTH_SHORT).show();
    }
}

manifest: UPDATE: After adding <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mamrezo.mapapp" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name="com.example.mamrezo.mapapp.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".ConnectionChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

</application>

</manifest>
Squonk
  • 48,735
  • 19
  • 103
  • 135
mamrezo
  • 51
  • 1
  • 7
  • Are you sure the receiver is being registered? Do you have the correct `` setting in your AndroidManifest.xml? – Squonk Dec 06 '14 at 08:57
  • Have a look at this [link](http://stackoverflow.com/questions/6165070/receiver-not-registered-exception-error). Do you unregister it somewhere else and is it possible you are unregistering again? Are you pausing(and you have unregistering code in onPause()) and then starting again and you forgot to put the register in onResume()? – SummerCode Dec 06 '14 at 08:44
  • @mamrezo : Hmm, OK. Does the receiver work? In other words do you see the `Toast` when network changes occur? – Squonk Dec 06 '14 at 09:12
  • @mamrezo : Edit your question to show the logcat data for when you get that error. – Squonk Dec 06 '14 at 09:18
  • @mamrezo : Why have you added BOOT_COMPLETED? – Squonk Dec 06 '14 at 11:56
  • @Squonk yes i know i add `` now it is working fine – mamrezo Dec 06 '14 at 12:34
  • wow I am person number 4 to have this issue.. I also seem to have in my code: extends BroadcastReceiver. what did you do to fix it? – hamish Feb 20 '16 at 20:24

1 Answers1

1

I always register the receiver in the onStart and unregister it in the onStop. I knew this thanks to this answer : https://stackoverflow.com/a/24391706/1203797

If its still not working in your case, simply wrapunregisterReceiver(connectionChangeReceiver); a try-catch block would be the answer, as stated in the link i posted above.

Or you can register it in the manifest, example :

    <receiver android:name=".ConnectionChangeReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129