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