Issue
I have an android application, i need one function or any thing that can check if the app is closed not the activity is closed. I have an App like this, If you use App for 10 seconds, wherever you are in any activity, you will receive the notification "Time out". I use the timer to do that, every thing work fine for the start. However, when I close app, the app must save the timeLeft for the next start. For example: If i have only 5 seconds left, then i close App. Next time i open the app, the timer will start from 5 seconds, not from 10 seconds. There are no problem if i stayed at WelcomeFirst (Activity which init the Timer), but if i go to next activity, the Timer will be canceled. I do not want that, The timer will only be canceld when the App is closed.
Is there a way to check if the App is closed or Activity is closed? I use ActivityLifecycleCallbacks and application class. I also try this function
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
}
It doesn't work.
My code
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;
}
Solution
Use the processLifeCyclerManager will solve your problem. https://medium.com/trendyol-tech/android-architecture-components-processlifecycleowner-26aa905d4bc5
Answered By - John
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.