简体   繁体   中英

How to make an activity window stay always on top

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

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 :)

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:

You can't. As this is defined in the Android system.

Follow the steps to achieve your requirement

  1. Create an activity which is going to be the top activity
  2. Add the following code in the manifest

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  3. Use the following code to get overlay permission from user

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 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(); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM