0

I have a thread in my callback function as follows:

@Override
public void onConnectError(final BluetoothDevice device, String message) {
    Log.d("TAG","Trying again in 3 sec.");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                   //Do something 
                }
            }, 2000);
        }
    });
}

I will to close the the above thread when I press the back button or onDestroy. How can I do it. Thank you

@Override
public void onBackPressed() {
    // Close or distroy the thread
 }
@Override
public void onDestroy() {
    // Close or distroy the thread
 }
John
  • 2,838
  • 7
  • 36
  • 65

2 Answers2

1

I'm mostly use thread in this way.See its independent in activity

public class TestActivity extends Activity {

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

    holdConnectionHandler.sendEmptyMessage(0);

}

Handler holdConnectionHandler = new Handler() {

public void handleMessage(android.os.Message msg) {
// do some work 

    holdConnectionHandler.sendEmptyMessageDelayed(0, 10 * 1000);
}
};

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

holdConnectionHandler.removeCallbacksAndMessages(null);
// or 
holdConnectionHandler.removeMessages(0);

}
}

Thanks hope this will help you

Saveen
  • 4,120
  • 14
  • 38
  • 41
  • holdConnectionHandler is a local variable in `ononConnectError`. How can I call it in my o`nDestroy` function – John Sep 15 '16 at 15:23
  • its not local, you can use it like any method, try it once and let me know if you stuck anywhere. – Saveen Sep 15 '16 at 15:27
  • Could you see my main function in onConnectError and use for method in that function? I will test it – John Sep 15 '16 at 15:29
  • you want to do something that depends on you variables BluetoothDevice device, message ? – Saveen Sep 15 '16 at 15:33
  • please check i have updated my answer. How it work in activity. Handler independently work. – Saveen Sep 15 '16 at 15:40
  • Start it in your onconnectionerror and call it on destroy with check if it does not equal to null then remove call backs – Saveen Sep 15 '16 at 15:43
1

Please do this like

private Handler handler;
private Runnable runnable;

@Override
public void onConnectError(final BluetoothDevice device, String message) {
Log.d("TAG","Trying again in 3 sec.");
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        handler = new Handler();
runnable = new Runnable() {
        @Override
        public void run() {
//Do something
        }
    };
handler.postDelayed(runnable, 2000);
    }
});
}

and

@Override
public void onBackPressed() {
if (handler != null && runnable != null) {
        handler.removeCallbacks(runnable);
    }
 }

and same in onDestroy();

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30