无法中断线程android

时间:2015-11-13 03:50:09

标签: java android multithreading notifications

我在onCreate上创建的一个线程,我在onDestroy上打断,StopTimer()继续运行。该活动位于onStop时,该线程用于将计时器作为通知运行。我希望线程在活动被销毁时停止。相反,它一直运行,直到我强制关闭应用程序。提前感谢您的帮助。

package com.example.ariel.skillmastery.controller;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.support.v4.app.NotificationCompat;

import com.example.ariel.skillmastery.R;
import com.example.ariel.skillmastery.model.User;

public class NotificationThread {

    Context context;
    private User user = MainActivity.getUserSingleton();
    NotificationCompat.Builder builder;
    String targetTime;
    Intent intent;
    boolean running = true;
    Thread notThread;

    public NotificationThread(Context context,String targetTime){
        this.context = context;

        this.targetTime = targetTime;

        builder = new NotificationCompat.Builder(context);
        builder.setContentTitle("Session Running");
        //builder.setContentText("Your quote of the day is ready!");
        builder.setSmallIcon(R.mipmap.ic_launcher);

        intent = new Intent(context, SessionActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        builder.setContentIntent(pendingIntent);

    }

            Runnable runnable = new Runnable() {

        int target;

        @Override
        public void run() {
            while (running){
                int incr;
                    try {
                        target = Integer.parseInt(targetTime) * 60;
                    } catch (Exception e) {
                        target = 99999;
                        e.printStackTrace();

                }

                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                for (incr = 0; incr <= target; incr += 1) {

                    if (target == 99999) {
                        builder.setProgress(0, 0, true);

                        builder.setContentText(user.setTime(incr));
                        notificationManager.notify(2, builder.build());
                    } else {
                        builder.setProgress(target, incr, false);
                        builder.setContentText(user.setTime(incr) + " Target Time: " + user.setTime(target));
                        notificationManager.notify(2, builder.build());
                    }

                    try {
                        // Sleep for 1 seconds
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println("error in AlarmBroadCastReceiver");
                    }
                }
                // When the loop is finished, updates the notification
                builder.setContentText("Target reached")
                        // Removes the progress bar
                        .setProgress(0, 0, false);
                builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
                notificationManager.notify(2, builder.build());

            }
        }
        };
    protected void startTimer(){
        running = true;
        notThread = new Thread(runnable);
        notThread.start();

    }

    protected void stopTimer(){
        running = false;
        notThread.interrupt();
    }

以下是活动:

 @Override
    protected void onDestroy() {
        super.onDestroy();
        notificationThread.stopTimer();



    }

    @Override
    protected void onStop() {
        super.onStop();

        notificationThread.stopTimer();
    }
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_session);
        notificationThread = new NotificationThread(this,targetTime);
        notificationThread.startTimer();
}

1 个答案:

答案 0 :(得分:0)

private volatile boolean running = true;

不保证Thread会看到使用此

运行的新布尔值replace

修改

将所有代码放入此try / catch块

try {
     // Sleep for 1 seconds
     Thread.sleep(1000);
    } catch (InterruptedException e) {
       System.out.println("error in AlarmBroadCastReceiver");
     }