Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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 设置倒计时以运行onDestroy()的正确方法是什么?_Java_Android_Android Studio_Sharedpreferences_Countdowntimer - Fatal编程技术网

Java 设置倒计时以运行onDestroy()的正确方法是什么?

Java 设置倒计时以运行onDestroy()的正确方法是什么?,java,android,android-studio,sharedpreferences,countdowntimer,Java,Android,Android Studio,Sharedpreferences,Countdowntimer,我正在尝试创建一个计时器,一旦触发onDestroy(),它将在后台启动,并且一旦计时器达到6小时,SharedReferences将用于在应用程序中进行更改 我在想。。。对于ondestory(),正确的设置方法是什么 如果需要,我会回答任何问题。谢谢。大约一周后,一个有效的答案出现了。这个答案包括与如何运行服务相关的内容。在这个场景中,我正在构建一个CountDownTimer服务 AndroidManifest.xml <service android:name=".TimeServ

我正在尝试创建一个计时器,一旦触发
onDestroy()
,它将在后台启动,并且一旦计时器达到6小时,
SharedReferences
将用于在应用程序中进行更改

我在想。。。对于
ondestory()
,正确的设置方法是什么


如果需要,我会回答任何问题。谢谢。

大约一周后,一个有效的答案出现了。这个答案包括与如何运行服务相关的内容。在这个场景中,我正在构建一个
CountDownTimer
服务

AndroidManifest.xml

<service android:name=".TimeService"/>
TimeService.java

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

public class TimeService extends Service {

   CountDownTimer cdt = null;
   private SharedPreferences pref;
   //Things you want SharedPreferences to change.
   Intent i;

   @Override
   public void onCreate() {
       i = new Intent(this, TimeService.class);
       startService(i);
       pref = this.getSharedPreferences("myAppPref", MODE_PRIVATE);
       cdt = new CountDownTimer(3600000, 1000) { //One hour timer with one second interval.

           @Override
           public void onTick(long millisUntilFinished) {

           }

           @Override
           public void onFinish() {
                //Whatever you need SharedPreferences to change here.
           }
       };
       cdt.start();
   }

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

你试过使用服务吗?这不是关于使用服务,更多的是关于在这个场景中建立倒计时的代码是什么样子的。这都是关于服务的。这是做你想做的事情的唯一方法。我搜索了一些类似于我的问题,关于如何创建倒计时服务,但它并没有真正解释如何在应用程序关闭时启动它。您可以使用
ContextCompat.startForegroundService(context,serviceentent)
,它将在API 26+上作为前台服务启动,在API 25-上作为普通服务启动。
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class TimeService extends Service {

   CountDownTimer cdt = null;
   private SharedPreferences pref;
   //Things you want SharedPreferences to change.
   Intent i;

   @Override
   public void onCreate() {
       i = new Intent(this, TimeService.class);
       startService(i);
       pref = this.getSharedPreferences("myAppPref", MODE_PRIVATE);
       cdt = new CountDownTimer(3600000, 1000) { //One hour timer with one second interval.

           @Override
           public void onTick(long millisUntilFinished) {

           }

           @Override
           public void onFinish() {
                //Whatever you need SharedPreferences to change here.
           }
       };
       cdt.start();
   }

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