问题描述
我已经使用GCM实现了推送通知,当我收到通知时,我希望在一个已经创建自定义对话框的对话框中显示。
I have implemented the push notification using GCM and when i receive the notification i want to show in a dialog for which i have created a custom dialog.
现在,即使设备已锁定模式匹配或PIN码,我希望我的对话框出现。
Now, i want my dialog to appear even if the device is locked whether a pattern match or PIN.
我做了以下尝试,但没有积极的结果。
I have made following tries, but no positive result.
public void onAttachedToWindow() {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
还有
public void onAttachedToWindow() {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
+ WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
清单中的权限:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
,并添加了
android:showOnLockScreen="true"
为我想要的活动在屏幕锁定时显示。
for the activity to which i want to be shown when screen is locked.
请帮助。
推荐答案
参考链接
当设备被锁定时,您不会显示对话框,而不将程序作为管理员特权应用程序绑定。
I do not think you can display dialog when device is locked without binding your application as admin privileged app programatically.
因此,您必须将应用程序与设备管理员绑定。您可以从下载设备管理员样本。
So you have to bind your application with Device administrator. You can download device administrator sample from https://github.com/marakana/DevicePolicyDemo.
通过
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName demoDeviceAdmin = new ComponentName(context, DemoDeviceAdminReceiver.class);
devicePolicyManager.setMaximumTimeToLock(demoDeviceAdmin, 0);
然后启动您可以在其中显示对话框的活动,在这个窗口中这样
then launch your activity in which you can display you dialog in onattach window like this
@Override
public void onAttachedToWindow()
{
super.onAttachedToWindow();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
new AlertDialog.Builder(this).setMessage("Dialog Displaying").setNeutralButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
请让我知道是否有帮助。
Please let me know if it help.
这篇关于即使屏幕被锁定,如何显示对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!