Prevent an Android application from being 'minimized'

2

I do not know if minimizing would be the correct word, I think not ... Please correct me.

I have an application and want to prevent anyone from minimizing it by using the Home button, if someone presses Home, the application will be completely finished, including all the Activities that have been opened, and also need to execute a method when that application is finalized in these terms ...

How to do it?

Edit: It is an application that the user can select files to protect against access from other applications, these protected files can be accessed only by my application (it asks for login), while my application is running the files are unprotected, it needs to 'unprotect' them to read, many laymen click on the Home button and think the application has been finalized, I want to avoid this, I want to prevent the files from remaining unprotected because the application is still running, and I want to avoid another one person to take the user's cell phone and end up gaining access to the application and files because it is simply open

    
asked by anonymous 15.10.2014 / 21:43

1 answer

2

Android is particularly designed to make pause to an application when the home button is used.

Because of this, there are two methods to manage what should happen when someone uses home or navigates out of the application: onPause () and onResume () .

You can learn more about how they work in Pausing and Resuming an Activity .

By way of example, you can make a class of your that extends Activity to perform certain tasks when the application enters pause :

public class BaseActivity extends Activity {
    @Override
    protected void onPause() {
        // raio, usaram o botão 'home', deixa-me proteger os ficheiros!
    }

    @Override
    protected void onResume() {
        // epá, utilizador voltou, deixa desproteger os ficheiro!
    }
}

public class Activity1 extends BaseActivity {
    // ...
}

Also for your case, setting android:clearTaskOnlaunch="true" can be useful. so that activities is terminated when someone returns to the application.

Check if application in background

It may also be useful to know if the application is going to background , which allows us to react in a more supportive way.

The function below lets you check if the application is going to background . It can be called in the onPause() method of all Activity of the application:

 /**
   * Verifica se o aplicativo está sendo enviado para segundo plano
   * (ou seja, para trás da atividade de outra aplicação).       * 
   *
   * @param context O contexto
   * @return <code>true</code> se outro aplicativo vai estar acima deste.
   */
  public static boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
      ComponentName topActivity = tasks.get(0).topActivity;
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
        return true;
      }
    }

    return false;
  }

The function lacks extra permissions so you should include the following in your AndroidManifest.xml :

<uses-permission android:name="android.permission.GET_TASKS" />

Credits of this function for the user @peceps in SOEN in this answer .

    
15.10.2014 / 23:32