问题
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
回答1:
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
来源:https://stackoverflow.com/questions/18341922/android-launching-activity-on-top-of-other-apps