Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – How to check the App is closed or the Activity is closed?

Posted on November 14, 2022

Solution 1 :

Use the processLifeCyclerManager will solve your problem.
https://medium.com/trendyol-tech/android-architecture-components-processlifecycleowner-26aa905d4bc5

Problem :

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

Comments

Comment posted by David Wasser

What do you mean by “app is closed”? This concept doesn’t really exist in Android. Please be very specific about what you mean.

READ  [ANSWERED] java - How to get specific array value Cloud Firestore
Powered by Inline Related Posts

Comment posted by Nghia Huy Hoang

My app will be closed by press the X button.After 4 days, i realize, using proccessLifeCycleManager will solve my problem.

Comment posted by David Wasser

If you were able to solve your problem, please post an answer to your question and accept that answer. It will help others who may have a similar problem. Thanks.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress