Missing a call to unregister HapticFeedbackBroadca

2019-02-09 15:25发布

问题:

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)

回答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.



回答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);
}


回答3:

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



回答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);