你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中后台显示悬浮窗口的方法

[日期:2013-05-20] 来源:Linux社区  作者:shrekmu [字体:大 中 小]

2、异常原因-等级不够

在“frameworks\base\core\res\AndroidManifest.xml”里一看

    <!-- Allows an application to open windows that are for use by parts
        of the system user interface.  Not for use by third party apps. -->
    <permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW"
        android:label="@string/permlab_internalSystemWindow"
        android:description="@string/permdesc_internalSystemWindow"
        android:protectionLevel="signature" />

原来有个保护等级,baidu了一下有四个

・normal 的权限只要申请了就可以使用;

・dangerous 的权限在安装时需要用户确认才可以使用;

・signature 和 signatureorsystem 的权限需要使用者的 app 和系统使用同一个数字证书。

等级不够,用不了

3、其他尝试

既然“TYPE_VOLUME_OVERLAY”不能用,那就试试“TYPE_SYSTEM_ERROR”,异常是不报了,但是dialog显示不出来。

・后台Activity的Context不能显示

・BroadcastReceiver的Context也不行

・Service的Context可以显示。

4、Service中的代码

public void test() {
 View v = View.inflate(mContext, R.layout.volume_panel, null);
 AlertDialog.Builder b = new AlertDialog.Builder(mContext, R.style.selectorDialog);
 b.setView(v);
 AlertDialog d = b.create();

 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
 //d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY); 
 d.show();
 /* set size & pos */
 WindowManager.LayoutParams lp = d.getWindow().getAttributes();
 WindowManager wm = (WindowManager) mContext.getSystemService(mContext.WINDOW_SERVICE);
 Display display = wm.getDefaultDisplay();
 if (display.getHeight() > display.getWidth()) {
  //lp.height = (int) (display.getHeight() * 0.5); 
  lp.width = (int) (display.getWidth() * 1.0);
 } else {
  //lp.height = (int) (display.getHeight() * 0.75); 
  lp.width = (int) (display.getWidth() * 0.5);
 }
 d.getWindow().setAttributes(lp);
 Log.d("M1-service", "show()");
}

linux
相关资讯       Android悬浮窗口 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款