如何查看App是否关闭或者Activity是否关闭?
How to check the App is closed or the Activity is closed?
我有一个 android 应用程序,我需要一个功能或任何可以检查应用程序是否关闭而不是 activity 关闭的东西。我有一个这样的应用程序,如果你使用应用程序 10 秒,无论你在任何 activity,你都会收到通知“超时”。我使用计时器来做到这一点,一开始一切都很好。但是,当我关闭应用程序时,应用程序必须为下次启动保存 timeLeft。例如:如果我只剩下 5 秒,那么我关闭 App。下次我打开应用程序时,计时器将从 5 秒开始,而不是 10 秒。如果我留在 WelcomeFirst(Activity 初始化定时器)没有问题,但如果我去下一个 activity,定时器将被取消。我不想那样,只有在应用程序关闭时才会取消计时器。
有没有办法检查App是否关闭或Activity关闭?我使用 ActivityLifecycleCallbacks 和应用程序 class。我也试试这个功能
private boolean isAppRunning() {
ActivityManager m = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningTaskInfo> runningTaskInfoList = m.getRunningTasks(10);
Iterator<ActivityManager.RunningTaskInfo> itr = runningTaskInfoList.iterator();
int n=0;
while(itr.hasNext()){
n++;
itr.next();
}
if(n==1){ // App is killed
return false;
}
return true; // App is in background or foreground
}
没用。
我的代码
public class App extends Application implements Application.ActivityLifecycleCallbacks {
private Long START_TIME_IN_MILLIS = 6000;
private Boolean mTimerRunning = false;
private Long mTimeLeftInMillis = START_TIME_IN_MILLIS;
private CountDownTimer mCountDownTimer;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityStarted(Activity activity) {
//if the activity is WelcomeFirst then init the timer
if (activity.localClassName == "WelcomeFirst") {
SharedPreferences prefs = getSharedPreferences("prefsDay", Context.MODE_PRIVATE);
mTimeLeftInMillis = prefs.getLong("millisLeft", 10000);
mTimerRunning = prefs.getBoolean("timerRunning", false);
startTimer();
}
}
@Override
public void onActivityStopped(Activity activity) {
//The code below mean if the Activity WelcomeFirst is closed then save the millisLeft for the next start.
if (activity.localClassName == "WelcomeFirst") {
mCountDownTimer.cancel();
mTimerRunning = false;
SharedPreferences prefs = getSharedPreferences("prefsDay", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putLong("millisLeft", mTimeLeftInMillis);
editor.putBoolean("timerRunning", mTimerRunning);
editor.apply();
}
//I want to make the change. If The App is closed, then save the millisLeft for the next start.
//Not the Activity is closed, then save the millisLeft for the next start
}
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
}
@Override
public void onFinish() {
mTimerRunning = false;
Toast.makeText(this, "Time Out",Toast.LENGTH_SHORT).show()
}
}.start();
mTimerRunning = true;
}
使用 processLifeCyclerManager 将解决您的问题。
https://medium.com/trendyol-tech/android-architecture-components-processlifecycleowner-26aa905d4bc5
我有一个 android 应用程序,我需要一个功能或任何可以检查应用程序是否关闭而不是 activity 关闭的东西。我有一个这样的应用程序,如果你使用应用程序 10 秒,无论你在任何 activity,你都会收到通知“超时”。我使用计时器来做到这一点,一开始一切都很好。但是,当我关闭应用程序时,应用程序必须为下次启动保存 timeLeft。例如:如果我只剩下 5 秒,那么我关闭 App。下次我打开应用程序时,计时器将从 5 秒开始,而不是 10 秒。如果我留在 WelcomeFirst(Activity 初始化定时器)没有问题,但如果我去下一个 activity,定时器将被取消。我不想那样,只有在应用程序关闭时才会取消计时器。
有没有办法检查App是否关闭或Activity关闭?我使用 ActivityLifecycleCallbacks 和应用程序 class。我也试试这个功能
private boolean isAppRunning() {
ActivityManager m = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningTaskInfo> runningTaskInfoList = m.getRunningTasks(10);
Iterator<ActivityManager.RunningTaskInfo> itr = runningTaskInfoList.iterator();
int n=0;
while(itr.hasNext()){
n++;
itr.next();
}
if(n==1){ // App is killed
return false;
}
return true; // App is in background or foreground
}
没用。
我的代码
public class App extends Application implements Application.ActivityLifecycleCallbacks {
private Long START_TIME_IN_MILLIS = 6000;
private Boolean mTimerRunning = false;
private Long mTimeLeftInMillis = START_TIME_IN_MILLIS;
private CountDownTimer mCountDownTimer;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityStarted(Activity activity) {
//if the activity is WelcomeFirst then init the timer
if (activity.localClassName == "WelcomeFirst") {
SharedPreferences prefs = getSharedPreferences("prefsDay", Context.MODE_PRIVATE);
mTimeLeftInMillis = prefs.getLong("millisLeft", 10000);
mTimerRunning = prefs.getBoolean("timerRunning", false);
startTimer();
}
}
@Override
public void onActivityStopped(Activity activity) {
//The code below mean if the Activity WelcomeFirst is closed then save the millisLeft for the next start.
if (activity.localClassName == "WelcomeFirst") {
mCountDownTimer.cancel();
mTimerRunning = false;
SharedPreferences prefs = getSharedPreferences("prefsDay", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putLong("millisLeft", mTimeLeftInMillis);
editor.putBoolean("timerRunning", mTimerRunning);
editor.apply();
}
//I want to make the change. If The App is closed, then save the millisLeft for the next start.
//Not the Activity is closed, then save the millisLeft for the next start
}
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
}
@Override
public void onFinish() {
mTimerRunning = false;
Toast.makeText(this, "Time Out",Toast.LENGTH_SHORT).show()
}
}.start();
mTimerRunning = true;
}
使用 processLifeCyclerManager 将解决您的问题。 https://medium.com/trendyol-tech/android-architecture-components-processlifecycleowner-26aa905d4bc5