CountDownTimer服务在打开时崩溃

时间:2017-12-04 22:18:54

标签: java android android-studio

我正在尝试制作一个倒数计时器屏幕,如果我退出应用程序或更换屏幕或其他东西,它将继续倒计时。由于某种原因,它一直在崩溃说timeLeft为空。我无法弄清楚为什么会这样,因为我知道我的Countdown类中的时间变量就在那里。谢谢你的帮助!

倒计时活动

public class Countdown extends Activity {
public static String time;
public static String address;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_countdown);
    Intent confIntent = getIntent();
    time = confIntent.getStringExtra("time");
    address = confIntent.getStringExtra("address");
    LocalBroadcastManager.getInstance(this).registerReceiver(
            new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    TextView textView= findViewById(R.id.t1);
                    String timeLeftString = intent.getStringExtra("timeSent");
                    int timeLeft = Integer.parseInt(timeLeftString);
                    if(timeLeft>0) {
                        textView.setText("You have " + timeLeft + " minutes left");
                    }
                    else{
                        textView.setText("Y'all outta time, see ya again soon!");
                        killIt();
                    }
                }
            }, new IntentFilter(CountdownService.ACTION_LOCATION_BROADCAST)
    );
    Intent toService = new Intent(this, CountdownService.class);
    startService(toService);

}
@Override
protected void onResume() {
    super.onResume();
    TextView textView= findViewById(R.id.t1);
    textView.setText("You have " + CountdownService.toSend + " minutes left");
}

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

}

public void killIt(){
    stopService(new Intent(this, CountdownService.class));
}


}

倒计时服务

public class CountdownService extends Service{
public static int toSend=0;
public int time;
public static final String
        ACTION_LOCATION_BROADCAST = CountdownService.class.getName() + 
"LocationBroadcast";
public final String timeFromCD = Countdown.time;
public final String address = Countdown.address;

@Override
public void onCreate() {
super.onCreate();
    time = Integer.parseInt(timeFromCD);
    time = time*60000;
    new CountDownTimer(time, 5000) {
        public void onTick(long millisUntilFinished) {
            int timeLeftInt = (int) Math.ceil((double) millisUntilFinished / 60000);    //Whole number of minutes left, ceiling
            sendBroadcastMessage(timeLeftInt);
            toSend = timeLeftInt;
            if(timeLeftInt == 5){
                Notify("Not Done");
            }

        }

        public void onFinish() {
            sendBroadcastMessage(0);
            Notify("done");

            Response.Listener<String> response = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        //creating a jsonResponse that will receive the php json
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success) {

                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(CountdownService.this);
                            builder.setMessage("Login Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

            SpotAmountRequest spotAmountRequest = new SpotAmountRequest(address, "0", response);
            RequestQueue queue = Volley.newRequestQueue(CountdownService.this);
            queue.add(spotAmountRequest);

        }
    }.start();

}


private void sendBroadcastMessage(int timeSent) {
        Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
        intent.putExtra("timeSent", timeSent);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

}



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


private void Notify(String doneness){

    NotificationManager notificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, Map.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
    if(doneness.equals("done")) {
        Notification n = new Notification.Builder(this)
                .setContentTitle("Time to leave!")
                .setContentText("Your PrePark spot has expired, time to go home!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true)
                .build();

        notificationManager.notify(0, n);
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        // Vibrate for 500 milliseconds
        v.vibrate(1000);
    }

    else{
        Notification n = new Notification.Builder(this)
                .setContentTitle("Ya got 5 minutes left in your PrePark spot!")
                .setContentText("Better get going soon here")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true)
                .build();

        notificationManager.notify(0, n);
    }
}


}

1 个答案:

答案 0 :(得分:0)

您在从服务广播的意图中存储了int值,但是您正试图将其作为StringgetIntExtra你收到广播。将接收者更改为使用getStringExtra而不是int,或者将String转换为服务中的find,然后再将其存储在意图中。