Android:线程仍在运行,尽管服务已经破坏

时间:2013-07-17 03:16:49

标签: android multithreading service

我已经尝试构建一个在服务中使用线程的android应用程序,服务和线程运行良好,但是当我停止并销毁服务时,线程仍然存在,所以当我再次运行应用程序时,应用程序中有2个线程,所以我想知道如何破坏线程。 有人可以帮我解决问题吗?

这是我的服务类:

public class MyService extends Service{
    Handler handler;
    static String toast_msg;
    Thread t;
    private static final String TAG = "MyService";

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

    @Override
    public void onCreate() {
        Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {
       /* Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    //Note: You can start a new thread and use it for long background processing from here.*/
        toast_msg = "Horas";
        super.onStart(intent, startId);


        Toast.makeText(getApplicationContext(), toast_msg, Toast.LENGTH_LONG)
                .show();

         handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                super.handleMessage(msg);
                Toast.makeText(getApplicationContext(), toast_msg,
                        Toast.LENGTH_LONG).show();
            }
        };

        t = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try { 
                        t.sleep(5000);
                        // Toast.makeText(getApplicationContext(), "Horas",
                        // Toast.LENGTH_LONG).show();
                        handler.sendEmptyMessage(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        //t.interrupt();
        handler.removeCallbacks(t);
        super.onDestroy();
        t.currentThread().interrupt();
    }



}

2 个答案:

答案 0 :(得分:1)

t = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                try { 
                    if(flag){//here add a flag
                      return;
                    }
                    t.sleep(5000);
                    // Toast.makeText(getApplicationContext(), "Horas",
                    // Toast.LENGTH_LONG).show();
                    handler.sendEmptyMessage(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
    t.start();

public void onDestroy() {
    Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    //t.interrupt();
    handler.removeCallbacks(t);
    super.onDestroy();
    flag = false;//here set flag to false
}

答案 1 :(得分:0)

您可能需要考虑使用Service或IntentService,因为它们可以更好地管理线程,暂停,重新启动等。

请参阅Android IntentService文档或此其他StackOverflow问答环节。由于Android API / SDK提供了更好的线程/服务管理功能,因此现在没有太多要求开启您自己的主题。

这些更加稳定,优化,并且(在我看来)比使用原始线程并尝试自己管理它更整洁,更清洁,特别是如果你不适应你的应用程序突然被杀死,因为电池电量不足或用户做出意想不到的事情。

相关问题