Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重新打开“活动”屏幕时重新启动倒计时服务_Java_Android - Fatal编程技术网

Java 重新打开“活动”屏幕时重新启动倒计时服务

Java 重新打开“活动”屏幕时重新启动倒计时服务,java,android,Java,Android,我正在尝试在Android上制作一个倒计时屏幕,如果你退出应用程序或进入应用程序中的不同屏幕或其他任何地方,它将继续倒计时。我将其作为服务运行,但在重新打开活动时,我仍然遇到重新启动的问题。任何帮助都会很好。这是我的密码 服务等级 import android.app.Service; import android.content.Intent; import android.os.CountDownTimer; import android.os.IBinder; import 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>

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

Ohhhh,那么如果我从onResume中取出startService,它是否可以工作?或者我是否还需要在我的主要活动onCreate中添加startService?谢谢你的帮助!这与你的需要有关。如果希望在活动启动后启动服务,那么只需在onCreate方法中调用startService。如果你不想在应用程序进入后台时停止服务,那么从onPause方法中删除stopService。