停止Android服务创建的线程

时间:2013-06-03 12:24:36

标签: java android android-service

我目前有一个应用程序,按下按钮时启动服务,并在服务中创建一个线程。 然后,我有一个第二个按钮(按下第一个按钮后出现)应该关闭服务然后杀死线程,下面是我当前的代码,但服务似乎停止但是线程继续运行。

public class MainActivity extends Activity {
    private static  Button              lock            = null;
    private static  Button              unlock          = null;

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

        lock = (Button) this.findViewById(R.id.lock);
        unlock = (Button) this.findViewById(R.id.unlock);


        lock.setOnClickListener(btn_lock);
        unlock.setOnClickListener(btn_unlock);

        unlock.setVisibility(View.VISIBLE);
        lock.setVisibility(View.GONE);
        text.setVisibility(View.GONE);


        startService(new Intent(this, MainService.class));
    }


    private OnClickListener btn_lock = new OnClickListener() {
        public void onClick(View v) {
                unlock.setVisibility(View.VISIBLE);
                lock.setVisibility(View.GONE);

                startService(new Intent(MainActivity.this, MainService.class));

        }
    };

    private OnClickListener btn_unlock = new OnClickListener() {
        public void onClick(View v) {
                unlock.setVisibility(View.GONE);
                lock.setVisibility(View.VISIBLE);
                stopService(new Intent(MainActivity.this, MainService.class));
        }
    };


}

然后我的服务类看起来像:

public class MainService extends Service {

    Thread 1Thread;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        1Thread = new Thread() {
            public void run() {
                    while(true){
                        try {
                            Thread.sleep(180000); // 3 minutes
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Log.i("TEST", "Thread is still here!");
                }
            }
        };


    }

    @Override
    public void onDestroy() {
        1Thread.interrupt();
    }

    @Override
    public void onStart(Intent intent, int startId) {

        1Thread.start();
    }

}

希望有人可以提供帮助,如果您需要更多信息,请告诉我们!

2 个答案:

答案 0 :(得分:3)

  boolean mStatus = true;

   @Override
    public void onCreate() {
        1Thread = new Thread() {
            public void run() {
                while (mStatus) {
                        try {
                            Thread.sleep(180000); // 3 minutes
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            continue;

                        }
                        Log.i("TEST", "Thread is still here!");

            }
        };


    }



@Override
    public void onDestroy() {
         mStatus = false;
         1Thread.interrupt();
    }

答案 1 :(得分:0)

你的线程没有处理interrupt,在while循环中每次迭代检查线程是否为interrupted,如果是,则不要继续循环:

while(!Thread.currentThread().isInterrupted()){
                        try {
                            Thread.sleep(180000); // 3 minutes
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                            e.printStackTrace();
break; //BREAK here, as thread was interrupted
                        }
                        Log.i("TEST", "Thread is still here!");
}

同样点击第二个按钮,你应该打断这个帖子:

1Thread.interrupt();