美文网首页
Handler 弱引用

Handler 弱引用

作者: 皓皓amous | 来源:发表于2023-07-18 17:28 被阅读0次
    private static class MyHandle extends Handler {
        WeakReference<Activity> weakReference;
    
        public MyHandle(Activity activity) {
            weakReference = new WeakReference<Activity>(activity);
        }
    
        @Override
        public void handleMessage(Message msg) {
            Activity activity = weakReference.get();
            if (activity != null && activity instanceof TestMainActivity) {
                switch (msg.what) {
                    case START_RECORD:
                        isListener = true;
                        fileRecod = FileUtil.createFile("test.amr");
                        if (fileRecod != null) {
                            startRecord();
                        }
                        break;
                    default:
                        break;
                }
                super.handleMessage(msg);
            }
        }
    
    }
    
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myHandle != null) {
            myHandle.removeCallbacksAndMessages(null);
        }
    }
    
    
                    Message msg = myHandle.obtainMessage();
                    msg.what = START_RECORD;
                    myHandle.sendMessage(msg);

    相关文章

      网友评论

          本文标题:Handler 弱引用

          本文链接:https://www.haomeiwen.com/subject/egnnudtx.html