如何摆脱涉及BroadcastReceiver的Android应用中的内存泄漏

这是我的初始化程序的代码:

        public void Initialize(Mainactivity activity)
        {
            context = activity;
            store = activity.store;
            gui = activity.gui;
            store.Initialize(activity);
            initialized = true;  // note the this is initialized to TRUE
        }

这是我的类构造函数的代码:

        public TextReceiver()
        {
            Android.Util.Log.Debug("DEBUG","CREATED a receiver :" + counter.ToString());
            toKill = counter++;
        }

这是我的解决方法的代码:

        public void Refresh()
        {
            initialized = true;
            store = context.store;
            gui = context.gui;
        }

这是我的回调代码:

        public override void OnReceive(Context contextPass,Intent intent)
        {
            Android.Util.Log.Debug("DEBUG","receiving a text message: initialized - [" + initialized.ToString() + "]");
            if (initialized == false) { Refresh(); }  // this is my current work-around
                                                      // but I fear this leaks memory
            if (intent.HasExtra("pdus") && (ContextCompat.CheckSelfPermission(context,Manifest.Permission.ReadSms) == Permission.Granted))
            {
                Bundle MessageInfo = intent.Extras;
                Java.Lang.Object[] pdus = (Java.Lang.Object[])MessageInfo.Get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.Length];

                for (int i = 0; i < messages.Length; i++)
                {
                    SmsMessage toSend = null;
                    // Verizon phones use the string "3gpp2"
                    // Some other carriers use the sting
                    // "3gpp". If the version is not correct,// this section will throw an exeception
                    try   { toSend = SmsMessage.CreateFromPdu((byte[])(pdus[i]),"3gpp2"); }
                    catch { toSend = SmsMessage.CreateFromPdu((byte[])(pdus[i]),"3gpp");  }

                    if (store.Push(toSend) == true) // checks for duplicates
                    {
                        gui.Textsreceived();
                    }

                }
            }
        }

日志文件很大,但这是重要的部分:

11-13 14:10:50.984  Google Pixel 3  Error   26765   Bugle   zdt: Jibe SDK Service not available. Is the Jibe SDK service running? Did you call connect() and wait for the notification before calling an API function?

请注意,错误报告中提到了未运行的服务:

Is the Jibe SDK service running? 
Did you call connect() and wait for the notification before calling an API function?

这是我使用回调方法的结果吗?我会以某种方式不正确地调用回调吗?

我的解决方法可行,但是我担心它会泄漏内存,因为它会在每次回调时实例化一个新的BroadcastReciever,并且就我所知不会释放实例。

更多详细信息:

如何摆脱涉及BroadcastReceiver的Android应用中的内存泄漏

lcz263097130 回答:如何摆脱涉及BroadcastReceiver的Android应用中的内存泄漏

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3106001.html

大家都在问