java - 设置 CountDownTimer 来运行 onDestroy() 的正确方法是什么?

标签 java android android-studio sharedpreferences countdowntimer

我正在尝试创建一个计时器,一旦触发 onDestroy() 就会在后台启动,一旦计时器达到 6 小时,就会使用 SharedPreferences在应用程序中进行更改。

我想知道...为 onDestroy() 设置 CountDownTimer 或类似内容的正确方法是什么。

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

最佳答案

大约一周后,一个可行的答案出现了。该答案包括与如何使服务运行相关的内容。在这种情况下,我正在构建一个 CountDownTimer 服务。

AndroidManifest.xml

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

MainActivity.java

import android.content.Intent;
import android.support.v4.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
       Intent i;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           i = new Intent(this, TimeService.class);

           stopService(i); //To stop the service the next time the app is launched.
       }

       @Override
       protected void onDestroy() {
           launchService(); //Launches the service once when app shuts down.
           super.onDestroy();
       }

       public void launchService() { //How to launch the service, depending the phone's API.
            if(Build.VERSION.SDK_INT >= 26) {
                 startForegroundService(new Intent(this, TimeService.class));
            }
            else{
                Intent i;
                i = new Intent(this, TimeService.class);
                ContextCompat.startForegroundService(this, i);
            }
       }
}

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

关于java - 设置 CountDownTimer 来运行 onDestroy() 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52266433/

相关文章:

java - JPA 条件查询 : fields of the object doesn't mapped

java - 有效 web.xml 中缺少 web-fragment.xml 中的元素

java - 在单元测试中模拟 RxJava 的 Activity 生命周期

svn - 如何在 Android Studio 2.0 中将特定的 svn 修订从分支合并到主干

java - 如何更改特定内容类型的 HTTP header ?

java - Java 中对象集合的单例和垃圾收集器

Android 评分栏大小(不能使用 Widget.RatingBar.Small)

android - 如何解析 MediaPlayer : Error (1, -19)

android - 谷歌 GMS :play service conflict with firebase

Android Studio 添加@androidx.annotation 和@android.support.annotation 问题