How to make an activity window stay always on top

╄→гoц情女王★ 提交于 2019-12-22 05:24:24

问题


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

  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 >= 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!