IntentReciever泄漏方向更改

时间:2016-06-01 10:17:03

标签: android android-orientation resource-leak

我在CustomWebView中注册了SMS_RECIEVED broadcastreceiver。我已经负责在活动的生命周期中注册和取消注册接收者。一切都很好,直到方向发生变化。

我尝试在不同的网络浏览状态下设置注册/取消注册接收方,例如onAttachedToWindow() / onDetachedFromWindow()onPause() / onResume()(明确要求来自活动&#39) ; s onPause() / onResume()),onStart() / onStop()。但似乎没有什么能阻止IntentReceiver方向改变的泄漏。

这是我尝试过的代码:

CustomWebView.java

public class CustomWebView extends WebView {

    BroadcastReceiver reciver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                final Bundle bundle = intent.getExtras();
                try {
                    if (bundle != null) {
                        final Object[] pdusObj = (Object[]) bundle.get("pdus");
                        Log.v(tag, "Got PDUS Obj [" + pdusObj + "]");
                        }
                    }

                } catch (Exception e) {
                    Log.e(tag, "Exception smsReceiver" + e);
                }
            }
        };

    --------------------
    --------------------
    some code
    --------------------

    @Override
    public void onResume() {
        try {
            IntentFilter intents = new IntentFilter(
                    "android.provider.Telephony.SMS_RECEIVED");
            getContext().registerReceiver(reciver, intents);
            Log.i(tag, "Webview sms reciever registered");
        } catch (Exception e) {
            e.printStackTrace();
        }

        super.onResume();
    }

    @Override
    public void onPause() {
        try {
            getContext().unregisterReceiver(reciver);
            Log.i(tag, "Webview sms reciever unregistered");
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onPause();
    }



    // Tried this one too
    /* @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.i(tag, "Billdesk onAttach to window called");
        try {
            IntentFilter intents = new IntentFilter(
                    "android.provider.Telephony.SMS_RECEIVED");
            getContext().registerReceiver(reciver, intents);
            Log.i(tag, "Webview sms reciever registered");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.i(tag, "Billdesk onDetach From window to window called");
        try {
            getContext().unregisterReceiver(reciver);
            Log.i(tag, "Webview sms reciever unregistered");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } */

}

MainActivity.java

public class MainActivity extends FragmentActivity{

-----------
-----------
-----------

@Override
    protected void onPause() {
        if (this.customWebView != null)
            this.customWebView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        if (this.customWebView != null)
            this.customWebView.onResume();
        super.onResume();
    }

}

当方向改变时,我得到以下错误:

Activity com.abcd.xyz.ActivityClass has leaked IntentReceiver com.abcd.xyz.CustomWebView$2@4898758 that was originally registered here. Are you missing a call to unregisterReceiver()?

提前致谢。

1 个答案:

答案 0 :(得分:0)

由于设备屏幕旋转而发生此问题...

当手机旋转并且屏幕改变方向时,Android通常会破坏应用程序的现有活动和碎片并重新创建它们。 Android会这样做,以便您的应用程序可以根据新配置重新加载资源。

你的接收者在活动的最后一个对象或实例上注册。所以解决方案是保存接收器对象......

您可以通过覆盖公共对象onRetainNonConfigurationInstance()并在onCreate方法中调用getLastNonConfigurationInstance()来保存任何对象。

@Override
public Object onRetainNonConfigurationInstance() {
// Save the reciever obj here

return data;
}

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     data = getLastNonConfigurationInstance();
}

希望这对你有所帮助。