问题
I want to create an activity that stays always on top (like the modal windows or task manager in Windows) of the others activities. How can I do this on Android? Thanks
回答1:
You can't. As this is defined in the Android system.
回答2:
You can use the following code in the overridden onStop method of your activity:
@Override
protected void onStop(){
super.onStop();
Intent intent = new Intent(this, ClassNameOfYourActivity.class);
startActivity(intent);
}
Beauty problem: your activity will pop-up again if any other activity trying to claim the focus. So it's not a modal window.
And it's dangerous! You wont be able to handle the Android GUI, you'll be able to control only your application GUI. For instance, switching debug mode on-off, killing application (only over ADB), reaching system settings, etc. will be impossible. If you switch off the ADB and combine it with the auto start mechanism then you'll be in trap.
So you won't be popular if you share it with Play :)
回答3:
Depending on what exactly you are trying to do, you might be happy with windows that stay on top of other Activities.
Apps like Super Video client & Floating Notes are able to display floating windows above other activities. These questions might point you in the right direction:
- Creating a system overlay window (always on top)
- How to create always-top fullscreen overlay activity in Android
回答4:
Follow the steps to achieve your requirement
- Create an activity which is going to be the top activity
Add the following code in the manifest
uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"
Use the following code to get overlay permission from user
if(Build.VERSION.SDK_INT >= 23) { if (!Settings.canDrawOverlays(this)) { new AlertDialog.Builder(this) .setTitle("Permission Request") .setMessage("This app needs your permission to overlay the system apps") .setPositiveButton("Open settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); startActivityForResult(myIntent, 101); } }) .setNegativeButton(android.R.string.no, null) .show(); } }
来源:https://stackoverflow.com/questions/4291349/how-to-make-an-activity-window-stay-always-on-top