简体   繁体   中英

Android - Launching an activity from service after force stop / crash

I am developing a countdown timer application and want to launch the activity to start after time completes every thing is working fine but after I force stop the app it shows NullPointerException . I can't figure out why it is showing this. here is my java code

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;
  }

This function is of static nested class and all the views I am using are of outer class

you can use the timer and set the delay of 1 sec

    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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM