-
Notifications
You must be signed in to change notification settings - Fork 1.2k
8. 一行代码在应用后台弹出弹窗
Jun edited this page Jan 29, 2021
·
5 revisions
首先需要添加权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
然后启用这个属性:
.enableShowWhenAppBackground(true)
而且需要悬浮窗权限,不过XPopup都封装好,一行代码即可。
完整的示例代码如下:
//申请悬浮窗权限
XPopup.requestOverlayPermission(getContext(), new XPermission.SimpleCallback() {
@Override
public void onGranted() {
ToastUtils.showShort("等待2秒后弹出XPopup!!!");
ActivityUtils.startHomeActivity(); //应用退到后台
//等待2秒,显示弹窗
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new XPopup.Builder(getContext())
.enableShowWhenAppBackground(true) //允许在应用后台弹出
.asConfirm("XPopup牛逼", "XPopup支持直接在后台弹出!", new OnConfirmListener(){
@Override
public void onConfirm() {
startActivity(new Intent(getContext(), MainActivity.class));
}
}).show();
}
}, 2000);
}
@Override
public void onDenied() {
ToastUtils.showShort("权限拒绝需要申请悬浮窗权限!");
}
});
QQ Email: 16167479@qq.com
QQ: 16167479