I have made a thread for certain process, and I am running this thread with Progress Dialog simultaneously. when process complete, Progress Dialog will dismiss. until this there is no problem but when user will press back button of phone two times, activity will destroy and my process should be stop. so I have stopped thread in onDestroy(). but app is crashing at mt.stop()
my code and error log is as bellow. please give me any solution and also if possible I want reason behind this.
Using flag, process will check for stop only at start of each iteration. and I want to stop process eminently.
code
Button b;
ProgressDialog pd = null;
MyThread mt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.btn_start);
b.setOnClickListener(this);
}
public void onClick(View v) {
pd = ProgressDialog.show(this, "one", "two", true, true);
mt = new MyThread();
mt.start();
}
class MyThread extends Thread {
@Override
public void run() {
// assume this loop is my process.
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("log_tag", "" + i);
}
Log.v("log_tag", "process COmplited");
pd.dismiss();
super.run();
}
}
@Override
protected void onDestroy() {
Log.v("log_tag", "OnDestroy");
pd.dismiss();
mt.stop();
super.onDestroy();
}
error in log cat;
11-09 17:26:45.313: E/global(594): Deprecated Thread methods are not supported.
11-09 17:26:45.313: E/global(594): java.lang.UnsupportedOperationException
11-09 17:26:45.313: E/global(594): at java.lang.VMThread.stop(VMThread.java:85)
11-09 17:26:45.313: E/global(594): at java.lang.Thread.stop(Thread.java:1379)
11-09 17:26:45.313: E/global(594): at java.lang.Thread.stop(Thread.java:1344)
11-09 17:26:45.313: E/global(594): at com.example.threaddemo.MainActivity.onDestroy(MainActivity.java:53)
11-09 17:26:45.313: E/global(594): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
11-09 17:26:45.313: E/global(594): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
11-09 17:26:45.313: E/global(594): at android.app.ActivityThread.access$2900(ActivityThread.java:125)
11-09 17:26:45.313: E/global(594): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
11-09 17:26:45.313: E/global(594): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 17:26:45.313: E/global(594): at android.os.Looper.loop(Looper.java:123)
11-09 17:26:45.313: E/global(594): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 17:26:45.313: E/global(594): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 17:26:45.313: E/global(594): at java.lang.reflect.Method.invoke(Method.java:521)
11-09 17:26:45.313: E/global(594): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 17:26:45.313: E/global(594): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-09 17:26:45.313: E/global(594): at dalvik.system.NativeStart.main(Native Method)