重新打开活动屏幕时,CountdownTimer服务重新启动

时间:2017-12-04 16:55:47

标签: java android

我试图在Android上制作倒数计时器屏幕,如果您退出应用程序或转到应用程序中的其他屏幕或其他任何内容,它将继续倒计时。我将其作为一项服务运行但我仍然在重新打开活动时遇到问题。任何帮助都会很棒。这是我的代码。

服务类

import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable; 
import android.support.v4.content.LocalBroadcastManager;




public class CountdownService extends Service{
public static final String
    ACTION_LOCATION_BROADCAST = CountdownService.class.getName() + "LocationBroadcast";

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

    }

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

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

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

主要活动

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.os.Bundle;
import android.widget.TextView;

public class Countdown extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown);
LocalBroadcastManager.getInstance(this).registerReceiver(
        new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                TextView textView= findViewById(R.id.t1);
                int timeLeft = intent.getIntExtra("timeSent", 0);
                if(timeLeft>0) {
                    textView.setText("You have " + timeLeft + " minutes left");
                }
                else{
                    textView.setText("Y'all outta time, see ya again soon!");
                }
            }
        }, new IntentFilter(CountdownService.ACTION_LOCATION_BROADCAST)
);

}
@Override
protected void onResume() {
super.onResume();
startService(new Intent(this, CountdownService.class));
}

@Override
protected void onPause() {
super.onPause();
stopService(new Intent(this, CountdownService.class));
}
}

主要活动的XML

<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center">

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:textSize="32sp"

/>


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

可能发生这种情况是因为你在onResume()中开始倒计时服务而你正在onPause()中停止服务 而且,在您的服务的onCreate()方法中,您将从倒数计时器创建新实例。