3

My codes below:

 public class SimpleService extends Service {
    private MyThread myythread;
    public boolean isRunning = false;
    long interval=30000;
    @Override
    public IBinder onBind(Intent arg0) {
    return null;
    }
    @Override
    public void onCreate() {
    super.onCreate();
    myythread  = new MyThread(interval);
    }
    @Override
    public synchronized void onDestroy() {
    super.onDestroy();
    if(!isRunning){
    myythread.interrupt();
    myythread.stop();
    }
    }
    @Override
    public synchronized void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    if(!isRunning){
    myythread.start();
    isRunning = true;
    }
    }
    class MyThread extends Thread{
    long interval;
    public MyThread(long interval){
    this.interval=interval;
    }
    @Override
    public void run(){
    while(isRunning){
    System.out.println("Service running");
    try {
    fileUpload();
    Thread.sleep(interval);
    } catch (InterruptedException e) {
    isRunning = false;
    e.printStackTrace();
    }
    }
    }
    private void fileUpload() {
    String url=Environment.getExternalStorageDirectory()+"/new_Account.txt";
    if(checkInternetConnection()){
    boolean upload_status=new UploadToFtp().ftpUpload1(url, "new_Account.txt");
    }
    }
    }
    private boolean checkInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
    return true;
    }
    else
    {
    return false;
    }
    }
    }

When call

@Override
    public synchronized void onDestroy() {
    super.onDestroy();
    if(!isRunning){
    myythread.interrupt();
    myythread.stop();
    }
    }

then throw exception :

java.lang.UnsupportedOperationException

How to solve this exception. please help me anybody.

kablu
  • 629
  • 1
  • 7
  • 26

4 Answers4

2

As of JDK8, Thread.stop is really gone. It is the first deprecated method to have actually been de-implemented. It now just throws UnsupportedOperationException

http://cs.oswego.edu/pipermail/concurrency-interest/2013-December/012028.html http://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

Bobz
  • 2,394
  • 1
  • 19
  • 20
1

Thread.stop() is deprecated. Do not use this method. This is what is throwing the UnsupportedOperationException.

See here: http://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

Karakuri
  • 38,365
  • 12
  • 84
  • 104
1

You can follow the below link

Please refer my solution at: Why java.lang.UnsupportedOperationException when Thread stop?

The solution of using a TimerTask instead of the thread can server your purponse better and more optimized.

Even the cancellation would work :)

P.S: It also has information why stop is not working for you.

[Reference :][Unsupported operation exception in thread]2

Community
  • 1
  • 1
nurealam11
  • 537
  • 4
  • 16
0

i think you made mistake too in your code,you have to stop thread if it is running.

@Override
public synchronized void onDestroy() {
super.onDestroy();
if(isRunning){
myythread.interrupt();
myythread.stop();
}
}
Imtiyaz Khalani
  • 2,037
  • 18
  • 32