Android 在Service中弹出窗口及SYSTEM_ALERT_WINDOW权限解决方法
来源:互联网 发布:微信转换淘宝链接 编辑:程序博客网 时间:2024/05/28 05:18
方案一、
要在Service中弹出对话框可以弹出一个全局对话框,设置步骤如下
1、在开发中有时会用到全局弹出对话框但必须在manifest中申请权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2、创建Dialog
AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setIcon(R.drawable.logo_mini); 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) { Intent i=new Intent(CoreService.this,LoginActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); } }); alertDialog = builder.create(); alertDialog.setCancelable(false); alertDialog.setCanceledOnTouchOutside(false); alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); alertDialog.show();
注意要设置Dialog的Window类型为WindowManager.LayoutParams.TYPE_SYSTEM_ALERT。
方案二、
采用方案一带来的问题:
- 用户在安装应用时会询问用户是否授权问题
- 同时在小米手机上默认是禁止系统弹出框的,应用中的系统弹出框将不能够弹出
那能不能不申请系统权限,又能弹出提示框提示用户呢?
这里妨转换思路,既然系统弹出框弹不出,我们就不采用系统弹出框,给他弹个Activity替代。但这个时候注意,要再Service是中或者ApplicationContext中startActivity时要添加额外标志ntent.FLAG_ACTIVITY_NEW_TASK:
Intent i=new Intent(this,ActDialog.class);i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(i);//代码中ActDialog其实是一个Activity把主题给它应用@android:style/Theme.Dialog//把activity做的像一个Dialog的风格
这样解决了权限申请和小米手机全局Dialog默认无法展示的问题。
最后补充一下在方案一中也可以不申请权限就能弹出对话框,将弹出Window type 改为LayoutParams.TYPE_TOAST,但是这种类型的弹出框是不能接受事件处理的。
阅读全文
0 0
- Android 在Service中弹出窗口及SYSTEM_ALERT_WINDOW权限解决方法
- Android的Service中弹出窗口解决方法
- Android的Service中弹出窗口解决方法
- Android的Service中弹出窗口解决方法
- android在Service中弹出Dialog对话框
- Android在Service中弹出对话框(Dialog)
- Android在Service中弹出Dialog
- Android 获取栈顶activity并在service 中弹出全局dialog解决方法
- Android的Service中弹出窗口解决方…
- 在Service中弹出AlertDialog
- 在Service中弹出对话框
- android在Service中弹出Dialog对话框,即全局性对话框
- android在Service中弹出Dialog对话框,即全局性对话框
- android在Service中弹出Dialog对话框,即全局性对话框
- android在Service中弹出Dialog对话框,即全局性对话框
- android在Service中弹出Dialog对话框,即全局性对话框
- Android在Service中弹出对话框(Dialog),即全局性对话框
- android在Service中弹出Dialog对话框,即全局性对话框
- Nginx安装
- 栈的应用之表达式求值(二)
- C# WinForm 中 MessageBox的使用详解
- Netty实现长连接简单例子
- 【Android实测】WebView的JS注入
- Android 在Service中弹出窗口及SYSTEM_ALERT_WINDOW权限解决方法
- LeetCode 169. Majority Element
- 343. Integer Break 题解
- HTML 颜色色号
- hdu3613(Manacher)
- 不能将CSV复制到postgreSQL表中:列不接受空字符串
- document常用方法
- python mysql 1366, u"Incorrect string value: '\\xF0\\x9F\\x98\\x8A' for column 'content' at row 1"
- webservice和socket服务的区别