<permission android:name="android.permission.SYSTEM_ALERT_WINDOW"
android:label="@string/permlab_systemAlertWindow"
android:description="@string/permdesc_systemAlertWindow"
android:protectionLevel="signature|setup|appop|installer|pre23|development" />
如何获取该权限:
如果有系统签名,则只需要在AndroidManifest.xml 中注册既可
如果没有系统签名, 可以动态申请:
public static void getPermission(Activity activity) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + activity.getPackageName()))
activity.startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE)
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
if (!Settings.canDrawOverlays(this)) {
// You don't have permission
SystemAlertUtil.getPermission(this)
} else {
if (!showText) {
SystemAlertUtil.showSystemAlert(this)
} else {
SystemAlertUtil.showTextAlert(this)
}
}
}
}
获得system alert window权限后可以做什么?
1.可以startActivity in Background(可以是自己的activity ,也可以是其他应用的Activity) , 并且Activity处于顶层
2.可以弹出顶层弹框
public static void showSystemDialog(Activity activity) {
WindowManager.LayoutParams params = new WindowManager.LayoutParams()
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
AlertDialog.Builder builder = new AlertDialog.Builder(activity)
builder.setIcon(R.drawable.app_icon_your_company)
builder.setTitle("下线通知").setMessage("该账号在另一台Android设备上登录。")
.setPositiveButton("重新登陆", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do somthing
}
}).setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
AlertDialog alertDialog = builder.create()
alertDialog.setCancelable(false)
alertDialog.setCanceledOnTouchOutside(false)
alertDialog.getWindow().setType(params.type)
alertDialog.show()
}
static TextView textView
static WindowManager mWindowManager
public static void showSystemView(Activity mContext) {
textView = new TextView(mContext)
textView.setText("此为悬浮窗口View")
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20)
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
}
mLayoutParams.format = PixelFormat.RGBA_8888
mLayoutParams.gravity = Gravity.CENTER | Gravity.TOP
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics()
int layoutY = displayMetrics.heightPixels / 2
int layoutX = displayMetrics.widthPixels - textView.getMeasuredWidth()
mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
mLayoutParams.x = layoutX
mLayoutParams.y = layoutY
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)
mWindowManager.addView(textView, mLayoutParams)
}
public static void removeMessage(Context context) {
if (textView != null) {
if (mWindowManager != null) {
mWindowManager.removeView(textView)
}
textView = null
mWindowManager = null
}
}