Missing a call to unregister HapticFeedbackBroadcastReceiver receiver that I never registered

12,735

Solution 1

Solution 1.

If You have a ListView with a ItemClickListener You have to set the listener to null on overrive Method onPause.

listView.setOnItemClickListener(exampleListener);

Example: Declaration of your listener

private OnItemClickListener exampleListener = new OnItemClickListener() {}....

@Override
    public void onPause() {

        super.onPause();

        listView.setOnItemClickListener(null);

    }

@Override
    public void onResume() {

        super.onResume();
            if(listView != null){
                listView.setOnItemClickListener(exampleListener);
             }
    }

Solution 2 If You know what is your broadcastReceiver instance You have to unregister always un overrive method onPause.

@Override protected void onPause() {

    super.onPause();
    synchronized (this) {
        if(broadcastReceiverInstance != null){
            unregisterReceiver(broadcastReceiverInstance );
        }
    }

}

The second solution I know it does not work for you but I let it for reference.

I hope it helps You.

Solution 2

Here is a work around !! I have a Samsung tab 7" 4.2.2

Causes of problem:
1. 4.2.2
2. ListView defined in a Layout.xml
3. new ListView(this)

Fix:
1. do not declare a ListView in the Layout, instead declare a holder
2. use 'getApplication()' instead of 'this' 'new ListView(getApplication());'
3. addView(yourListView)

TheLayoutFile.xml

    <RelativeLayout
        android:id="@+id/listViewHolder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </RelativeLayout>

The ListView in your Activity

private void displayPostsInListView() {
    RelativeLayout listViewHolder = (RelativeLayout) findViewById(R.id.listViewHolder);
    listView = new ListView(getApplication());
    listViewHolder.addView(listView);
    listView.setOnItemClickListener(lineItemListener);

    adapter = new MyDisplayAdapter(this, listModel.getPostings());

    listView.setAdapter(adapter);
}

Solution 3

You must call unregisterReceiver() on any receivers you have registered in your Activity. This will typically be in onPause().

Solution 4

How do I identify which Receiver is causing the leak and eliminate the error?

The answer is in the LogCat you posted.

Activity com.ssowens.groovebasstrial.BassActivity has leaked IntentReceiver com.immersion.android.haptics.HapticFeedbackManager$HapticFeedbackBroadcastReceiver@41d166d0 that was originally registered here.

You registered a HapticFeedbackBroadcastReceiver in BassActivity, which needs to be unregistered when you pause or close the app. As @svenoaks says, you should override Activity.onPause() and unregister every receiver you've registered, using unregisterReceiver(referenceToReceiver);

Share:
12,735
sowens
Author by

sowens

Updated on June 14, 2022

Comments

  • sowens
    sowens almost 2 years

    I get the following error

    "Are you missing a call to unregister receiver"

    when I hit the back button to exit my application. How do I identify which Receiver is causing the leak and eliminate the error? I am using the code "DownLoader" from Google to download an expansion file.

    10-27 22:13:32.818: E/ActivityThread(30744): Activity com.ssowens.groovebasstrial.BassActivity has leaked IntentReceiver com.immersion.android.haptics.HapticFeedbackManager$HapticFeedbackBroadcastReceiver@41d166d0 that was originally registered here. Are you missing a call to unregisterReceiver()?
    10-27 22:13:32.818: E/ActivityThread(30744): android.app.IntentReceiverLeaked: Activity com.ssowens.groovebasstrial.BassActivity has leaked IntentReceiver com.immersion.android.haptics.HapticFeedbackManager$HapticFeedbackBroadcastReceiver@41d166d0 that was originally registered here. Are you missing a call to unregisterReceiver()?
    10-27 22:13:32.818: E/ActivityThread(30744):    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:800)
    10-27 22:13:32.818: E/ActivityThread(30744):    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:601)
    10-27 22:13:32.818: E/ActivityThread(30744):    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1650)
    10-27 22:13:32.818: E/ActivityThread(30744):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1630)
    10-27 22:13:32.818: E/ActivityThread(30744):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1624)
    10-27 22:13:32.818: E/ActivityThread(30744):    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:430)
    10-27 22:13:32.818: E/ActivityThread(30744):    at com.immersion.android.haptics.HapticFeedbackManager.setupPackageBroadcastReceiver(HapticFeedbackManager.java:564)
    10-27 22:13:32.818: E/ActivityThread(30744):    at com.immersion.android.haptics.HapticFeedbackManager.<init>(HapticFeedbackManager.java:108)
    10-27 22:13:32.818: E/ActivityThread(30744):    at com.immersion.android.HapticFeedbackManagerProxy.initialize(HapticFeedbackManagerProxy.java:90)
    10-27 22:13:32.818: E/ActivityThread(30744):    at com.immersion.android.HapticFeedbackManagerProxy.access$100(HapticFeedbackManagerProxy.java:30)
    10-27 22:13:32.818: E/ActivityThread(30744):    at com.immersion.android.HapticFeedbackManagerProxy$1$1.run(HapticFeedbackManagerProxy.java:71)
    10-27 22:13:32.818: E/ActivityThread(30744):    at java.lang.Thread.run(Thread.java:856)
    
  • sowens
    sowens over 10 years
    Thanks for the feedback. @Ole However, I have searched for HapticFeedbackBroadcastReceiver in my code and I don't find a match. I am assuming it is when I use the onTouchListener. So, to unregister it would I unregister the onTouchListener? Not sure how to unregister it.
  • Ole
    Ole over 10 years
    @sowens Are you testing your application on a Samsung device running 4.2.2? According to this, there seems to be a bug in Samsungs 4.2.2 build
  • sowens
    sowens over 10 years
    Yes, I am running a Samsung version 4.2.2. How unfortunate that this error is occurring. I didn't see any work around in the post.
  • sowens
    sowens over 10 years
    Thank you for your response to my question. However, see the comments above. Looks like my problem is specific to my version (4.2.2) of Android and the tablet (Samsung 7 inch) that I am using. @Schwertfisch
  • schwertfisch
    schwertfisch over 10 years
    @sowens I think your problem is with a ListView. if You have one the problem is thath you are not setting your listview listener override onPause. Sorry for my bad english. I'm going to edit the Answer. Tell me if does it works.
  • sowens
    sowens over 10 years
    I will give it a try. @bear
  • Kalle
    Kalle over 9 years
    You don't have to create a holder. It's simpler to just nullify the adapter in onStop() and reset as needed
  • MTurPash
    MTurPash about 9 years
    I'm having the same problem... But since i am also testing on a samsung tab 2 10.1 with android 4.2.2 i think it may be the same here ... Since the problem shows only errors in logcat und does not crash the app i think i will leave it for now.
  • JulianHarty
    JulianHarty over 7 years
    A tweak to @Schwertfisch 's approach - in the code I'm working on, the onItemClickListener(...) is called in onCreate() and a new object instantiated. This would be discarded and replaced in the proposed onResume(). So I've added the following guard code to test whether the onItemClickListener is null: @Override public void onResume() { super.onResume(); if (drawerList != null drawerList.getOnItemClickListener() == null) { drawerList.setOnItemClickListener(new DrawerItemClickListener()); } }
  • roberth
    roberth over 7 years
    @JulianHarty, is it necessary to call it in both OnCreate and OnResume? OnResume is also called after OnCreate, so having it there would be enough I suppose.
  • JulianHarty
    JulianHarty over 7 years
    @roberth I agree that in principle calling it in OnResume should be enough. In my case I've been working with someone else's code and didn't want to make major modifications to it so left the code in the OnCreate essentially unchanged.