Android - 强制停止/崩溃后从服务启动活动

时间:2018-05-04 03:37:46

标签: java android

我正在开发一个倒数计时器应用程序,并希望启动活动,在时间完成后启动每件事情都正常但在我强制停止应用程序后显示NullPointerException。我无法弄清楚为什么会这样。 这是我的java代码

public int onStartCommand(Intent intent, int flags, int startId) {
    handler = new Handler();
    x=1;
    //final TimerService timerService=this;
    r = new Runnable() {
        @Override
        public void run() {
            try {
                 if ((minutes >= 0) && (seconds >= 0) && (minutes <= 59) && (seconds <= 59)) {
                 if (seconds == 0 && minutes > 0) {
                     minutes--;
                     seconds = 59;
                 } else if (seconds == 0 && minutes == 0) {
                     try {
                         mins.setEnabled(true);
                         secs.setEnabled(true);
                         Intent ring = new Intent(getApplicationContext(), TimerComplete.class);
                         ring.setAction(Intent.ACTION_VIEW);
                         ring.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                         ring.setComponent(new ComponentName(getApplicationContext().getPackageName(), TimerComplete.class.getName()));
                         timerService.startActivity(ring);
                         stopSelf();
                         handler.removeCallbacks(this);
                     } catch (Exception e) {
                         Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                     }
                     mins.setText(String.format("%02d", minutes));
                     secs.setText(String.format("%02d", seconds));
                     if (seconds != 0) {
                         seconds--;
                         x++;
                         handler.postDelayed(this, 1000);
                     }
                } else {
                     mins.setEnabled(true);
                     secs.setEnabled(true);
                     mins.setText("00");
                     secs.setText("00");
                     handler.removeCallbacks(this);
                     Toast.makeText(getApplicationContext(), "Invalid Time", Toast.LENGTH_LONG).show();
               }
          }
     } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
      }
      }
      };
      handler.post(r);
      return START_REDELIVER_INTENT;
  }

此函数是静态嵌套类,我使用的所有视图都是外部类

1 个答案:

答案 0 :(得分:0)

  

您可以使用计时器并设置延迟1秒

    int myTimeCounter = 60;
    Timer myTimer = new Timer();
    private void startRepeatedTask() {
        myTimer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        if (myTimeCounter == 0) {
                            myTimer.cancel();
                            callActivity();
                            return;
                        }
                        //Log.d(TAG, "timer=" + String.valueOf(myTimeCounter));
                        myTimeCounter--;
                    }
                });
            }
        }, 0, 1000);
    }