3

Background: I am trying to build a Launcher app that will restrict users to a set of permitted applications only and will not allow them to access device settings & anything other then the permitted apps.

Why: This is a project for a private company to distribute devices to their employees with restricted usage.

Issue: I am able to detect launch of other apps, but i am not able to overlay my activity on top of them. Below is the code, i am trying after receiving broadcast of not allowed app.

private class BadAppReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context arg0, Intent arg1) {
         Intent i = new Intent(HomePage.this,ErrorClass.class);
         wh.topAct = wh.bad_app;
         HomePage.this.startActivity(i);
         Toast.makeText(HomePage.this, "Access to " + wh.bad_app + " is not allowed.",Toast.LENGTH_LONG).show();
     }

}

Interesting Fact: I am able to overlay if I just launch another app instead of trying to open an Activity from my app. Using this solution will make my app dependent on another app and kind of freezes the screen for couple of seconds.

Question: What am i missing or can do to pop my Activity over other apps.

Thanks in advance and your help is highly appreciated. - Pratidhwani

Thanks Umer, I have used your solution, but this is what is happening now, user opens settings from tray, my app gets the broadcast and shoots ErrorClass Activity, the Toast I have in place appears but ErrorClass Activity do not appear. So user is able to access the settings, now when user presses back button on settings, ErrorClass appears.

this is what I have in ErrorClass onCreate

        super.onCreate(savedInstanceState);

         WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY , 
                    WindowManager.LayoutParams. FLAG_DIM_BEHIND, PixelFormat.TRANSLUCENT);

         WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View myView = inflater.inflate(R.layout.error, null);

         wm.addView(myView, windowManagerParams);

        wh = ((Launcher) getApplication()).getvar();
        mHandlerReg.removeCallbacks(mUpdatereg);
        mHandlerReg.postDelayed(mUpdatereg,2000);   

Thanks!! - Pratidhwani

Pratidhwani
  • 33
  • 1
  • 4

1 Answers1

5

If you want to overlay an app on top of other apps, add the following code.

 WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY , 
            WindowManager.LayoutParams. FLAG_DIM_BEHIND, PixelFormat.TRANSLUCENT);

 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View myView = inflater.inflate(R.layout.youractivity, null);

 wm.addView(myView, windowManagerParams);

Add permission to Android Manifest:

SYSTEM_ALERT_WINDOW

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • Hi Umer, I have added results in my post above after using your solution. Can you please see and let me know what I am doing wrong here. Thanks for you help!! – Pratidhwani Aug 21 '13 at 04:26
  • A pictorial view/example of your app will be helpful to understand your problem – Umer Farooq Aug 21 '13 at 04:48
  • I am able to resolve my issue, your solution works for me. I was using it inside Activity thus it was not working, now I have used it inside Service and it works great. Thank you very much. – Pratidhwani Aug 21 '13 at 06:00
  • Just a note for others, to use above solution, your app must have - android.permission.SYSTEM_ALERT_WINDOW permission. – Pratidhwani Aug 21 '13 at 06:02
  • @Pratidhwani BTW what kind of app are you making? – Umer Farooq Aug 21 '13 at 07:47
  • Its a launcher app, restricting its users to a set of apps decided by admin. So if user tries to run any app other then the allowed ones, he should be directed back to launcher. – Pratidhwani Aug 21 '13 at 08:23
  • And what is the purpose of this overlay activity? – Umer Farooq Aug 21 '13 at 08:24
  • I get a window leaked exception when i press home or back button.. instead the view should always be visible – Srikanth Pai Nov 05 '13 at 09:45