【问题标题】:cant interrupt thread android不能中断线程android
【发布时间】:2015-11-13 03:50:08
【问题描述】:

我在 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();
}

【问题讨论】:

  • 线程是一个非常重量级的解决方案,用于 UI 中的简单间隔和延迟。您应该改用Handler
  • 您能否建议我如何使用处理程序来实现这一点?

标签: java android multithreading notifications


【解决方案1】:

private volatile boolean running = true;

不保证线程会看到新的布尔值替换运行

编辑

把你所有的代码放在这个 try/catch 块中

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

【讨论】:

  • 试过了,但我无法在尝试中 notify() 通知没有弹出。
猜你喜欢
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 2014-08-16
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多