This Handler class should be static or leaks might occur:AsyncQueryHandler

无人久伴 提交于 2020-01-01 02:49:14

问题


Handler reference leaks Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue. If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows: Declare the Handler as a static class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakReference object.

I need a solution!!!!!

public class EMG_Activity extends AppCompatActivity {

    // An object that manages Messages in a Thread
    public static Handler HandlerMessager;
    private static AsyncQueryHandler queryHandler;

    private static final String EMG = "EMG";
    private ManageConnectedSocket manageConnectedSocket;
    private Thread manageThread;
    private Button button_start_pause;


    private int id =  0;
    private int xValue = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_emg);

        queryHandler = new AsyncQueryHandler(getContentResolver()) {

            @Override
            protected void onInsertComplete(int token, Object cookie, Uri uri) {
                if(cookie != null)
                    id = (int) ContentUris.parseId(uri);
            }
        };
              ...........

        button_start_pause = (Button) findViewById(R.id.button_start_pause);
        button_start_pause.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                ..................

                manageConnectedSocket = new ManageConnectedSocket(MainActivity.bluetoothDevice, MainActivity.samplingFrequency, new int[]{0}, new int[]{1, 0, 1, 1});
                manageThread = new Thread(manageConnectedSocket);
                manageThread.start();

                ContentValues values = getContentValuesExam (EMG, AlsrmSchema.PROGRESS, Utils.getCurrentDate());
                queryHandler.startInsert(1, id, AlsrmContract.Exam.CONTENT_URI, values);

                ......
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        .....

            ContentValues values = getContentValuesExam (EMG, AlsrmSchema.CORRUPTED, Utils.getCurrentDate());
            queryHandler.startUpdate(1, null, AlsrmContract.Exam.CONTENT_URI, values, AlsrmSchema.id + " = ? ", new String[]{"" + id});
        }
    }
}

来源:https://stackoverflow.com/questions/37188519/this-handler-class-should-be-static-or-leaks-might-occurasyncqueryhandler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!