Skip to content Skip to sidebar Skip to footer

Stop Current Thread Issue

My Problem is I am Running one Method into Thread and after 30 Seconds I will display alertdialog and click on alertdialog's ok button i will stop current thread but problem is thr

Solution 1:

see this question...instead you can put a flag in run method.. and check whether to run or not to run the code in thread.

Solution 2:

try this:-

publicclassCountDownTimerActivityextendsActivityimplementsRunnable {

Thread t;

/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    t = newThread(this);
    t.start();

}

@Overridepublicvoidrun() {
    // TODO Auto-generated method stub
    myTimer.start();
    mDeclaration();
    myTimer.cancel();
}

privateCountDownTimermyTimer=newCountDownTimer(30000, 1000) {
    // This will give you 30 sec timer with each tick at 1 secondpublicvoidonTick(long millisUntilFinished) {

    }

    publicvoidonFinish() {

        AlertDialog.Builderalert=newAlertDialog.Builder(
                CountDownTimerActivity.this);
        alert.setMessage("Loading...");
        alert.setPositiveButton("OK",
                newDialogInterface.OnClickListener() {

                    @OverridepublicvoidonClick(DialogInterface dialog, int which) {
                    //  t.interrupt();
        t.stop();
                    }
                });
        alert.show();
    }
};

publicvoidmDeclaration() {
    for (inti=0; i < 100000; i++) {
        System.out.println("Printing OK" + i);
    }
}

}

Post a Comment for "Stop Current Thread Issue"