close Android APP from a class

3

I created a class

public class MyCountDownTimer extends CountDownTimer {
    Activity activity;
    public MyCountDownTimer(long startTime, long interval,Activity m_activity) {
        super(startTime, interval);
        activity = m_activity;
    }

    @Override
    public void onFinish() {
        //DO WHATEVER YOU WANT HERE
        FunctionsUtil.ShowMessage("TERMINO SU TIEMPO",Toast.LENGTH_LONG);
        valoresGenerales.banderaCountDownTimer = false;
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if( millisUntilFinished/1000 == 35)
        {
            FunctionsUtil.ShowMessage("SI no usa  en " + millisUntilFinished/1000 + " segundos se cerrara automáticamente.",Toast.LENGTH_LONG);
        }
        if( millisUntilFinished/1000 == 10)
        {
            FunctionsUtil.ShowMessage("SI no usa  en " + millisUntilFinished/1000 + " segundos se cerrara automáticamente.",Toast.LENGTH_LONG);
        }


    }
}

as they know MyCountDownTimer .. is a counter back .. what I try to do is ... close my app when this counter reaches 0 and enter onFinish () I put the code

android.os.Process.killProcess(android.os.Process.myPid());

but doing tests I said that only closes a single activity (which is visible) and opens the actMain (main) automatically.

but I am my app has several activitys that open (activity 1 opens to activity2 that asu opens to activity 3 etc) that means that I have more than one activity in use and therefore the code does not help me.

what code could I use ???

thank you very much.

    
asked by Gerard_jcr 03.05.2016 в 18:38
source

2 answers

4

I give you a solution in which your activities have to extend from a "parent" activity, for example, we suppose to have these 3 Activities, the 3 should extend the parent activity ParentActivity :

public class MainActivity extends ParentActivity {

public class Activity2 extends ParentActivity {

public class Activity3 extends ParentActivity {

This would be the activity code ParentActivity , which has 2 methods setActivity() to add the reference of the activity to a List and quitApp() that searches all the activities that were saved in the list and ends them:

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;    
import java.util.ArrayList;
import java.util.List;

public class ParentActivity extends AppCompatActivity {


    List<Activity> activities = new ArrayList<Activity>();

    public void setActivity(Activity act){
        activities.add(act);
    }

    public void quitApp(){
        for(Activity a : activities){
            a.finish();
        }
    }    

}

Within your activities you have to add the line:

setActivity(this);

to add the activity to List .

and in anyone you can call the method:

quitApp();

with which you will close all Activities registered in List defined in ParentActivity .

In your code you could call quitApp() within onFinish() to close all Activities:

    @Override
    public void onFinish() {
       quitApp();
    }
    
answered by 03.05.2016 в 21:56
1

When the time runs out, have the main activity loaded on top of all the others.

//Enviar itent para cerrar app
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("SalirApp", true);
startActivity(intent);
finish();

and in your main activity in onCreate

protected void onCreate(Bundle savedInstanceState) {
...
  if( getIntent().getBooleanExtra("SalirApp", false)){
      finish();
      return;
  }
    
answered by 03.05.2016 в 21:11