androidandroid-studioandroid-4.2-jelly-bean

How make a Android application that can't be closed




I'm developing an Android app for a company where those who will use it are the employees, for this reason the company asked me to develop an application that the user can not close, because if he will not use the smartphone for other purposes, but for this I need the Android native buttons do not interfere with the application.

I've deactivated the button to go back and put the application as Home.

@Override
     public void onBackPressed () {
         super.onBackPressed ();
     }

...

<category android: name = "android.intent.category.HOME" />

However if the user clicks the button that displays open applications, it can exit the application.

I researched a lot before creating resolve this question and realized several attempts to solve this problem.

One. I tried to create the same behavior as the MX Player has, when you use the lock to see a video, the MX Player is always on top of everything, leaving the user to see and click others places. However using this behavior does not i cant see My Dialogs nor Popup and also can not apply a thema, as in my case is not simply an activity is the entire application.

Reference links of my attempt
How to disable Home and other system buttons in Android?
http://www.piwai.info/chatheads-basics/

If anyone knows how to use that behavior MX Player, or if anyone knows any more how to make the user can not close the application, please help me, I know it's not just me who have this problem.

Any help is welcome!

My API is 16 - Android-4.1


Solution

  • The best way i found to the user can't access others apps, was to make a service that check which is the top activity, if don't is my then reopen my app.

    ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTasks = manager.getRunningTasks(1);
    
    if (runningTasks != null && runningTasks.size() > 0) {
        ComponentName topActivity = runningTasks.get(0).topActivity;
    
        if (!topActivity.getPackageName().startsWith("com.mypackage.")) {
            Log.i("Top Activity", topActivity.getPackageName());
            if (LocalCache.getInstance().isForceHome()) {
                Intent intent = new Intent(HomeService.this, AuthLoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
        handler.postDelayed(this, 500);
    }