电信A库要求android系统中有多个Browser时,开机自动设置一个默认浏览器,而不用弹出选择框让用户手动选择。

  监听开机广播Intent.ACTION_BOOT_COMPLETED, 用PackageManager调用addPreferredActivity来设置默认Browser。因为要有系统权限才能设置成功,所以在Package/apps/Settings模块中添加代码实现。(系统通过校验发起调用PackageManager.addPreferredActivity程序的Uid来判断是否有权限)

 public class BootCompletedReceiver extends BroadcastReceiver {

     private final static String PKG_NAME = "com.android.browser";
private final static String CLASS_NAME = "com.android.browser.BrowserActivity"; private final static String[] SCHEME = new String[] { "http", "https", "about", "javascript", }; @Override
public void onReceive(Context c, Intent paramIntent) {
try {
Log.d("antoon", paramIntent.getAction());
setDefaultBrowser(c);
} catch (Exception e) {
e.printStackTrace();
}
} private void setDefaultBrowser(Context c) { long start = System.currentTimeMillis(); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://"));//这里如果配置 “file://” pm.queryIntentActivities得到的list为空,不知为何 final int flags = PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER; PackageManager pm = c.getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, flags); final int N = list == null ? 0 : list.size();
if (N == 0)
return; int bestmatch = 0;
int uid = -1;
ComponentName matchComponentName = null;
ComponentName[] set = new ComponentName[N]; ComponentName defaultComponentName = new ComponentName(PKG_NAME, CLASS_NAME);
for (int i = 0; i < N; i++) {
ResolveInfo r = list.get(i);
ComponentName cp = new ComponentName(r.activityInfo.packageName, r.activityInfo.name);
set[i] = cp;
if (cp.equals(defaultComponentName)) {
bestmatch = r.match;
matchComponentName = cp;
uid = r.activityInfo.applicationInfo.uid;
}
Log.d("antoon", i + " -- " + cp);
} if (matchComponentName == null) {
Log.d("antoon", "matchComponentName is null");
matchComponentName = set[0];
bestmatch = list.get(0).match;
} Log.d("antoon", "bestmatch = " + bestmatch + " matchComponentName = "
+ matchComponentName); // List<IntentFilter> filterList = filterList();
62 //for (IntentFilter filter : filterList) {
63 // pm.addPreferredActivity(filter, bestmatch, set, matchComponentName);//用下面的方法也可以,系统权限的校验就是根据CallingUserId来判断
64 // pm.addPreferredActivity(filter, bestmatch, set, matchComponentName, android.os.UserHandle.getCallingUserId());
65 //}
       pm.addPreferredActivity(getFilterViewHttp(), bestmatch, set, matchComponentName, android.os.UserHandle.getCallingUserId());
Log.d("antoon", "used time :" + (System.currentTimeMillis() - start)+"ms");
}       /*没必要针对每个scheme创建filter,在一个filter中加入所有scheme就可以,与Browser中AndroidManifest.xml中的配置对应。
      public List<IntentFilter> filterList() {
70   ArrayList<IntentFilter> list = new ArrayList<IntentFilter>();
71   for (String sche : SCHEME) {
72   IntentFilter filter = new IntentFilter();
73   filter.addAction(Intent.ACTION_VIEW);
74   filter.addCategory(Intent.CATEGORY_BROWSABLE);
75   filter.addCategory(Intent.CATEGORY_DEFAULT);
76   filter.addDataScheme(sche);
77   list.add(filter);
78   }
79   return list;
80   }
      */
    
     public IntentFilter getFilterViewHttp() {

            IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_VIEW);
filter.addCategory(Intent.CATEGORY_BROWSABLE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addDataScheme("http"); return filter;
}
 }
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

看看系统Browser的配置

    <activity
android:name="BrowserActivity"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
android:label="@string/application_name"
android:launchMode="singleTask"
android:theme="@style/BrowserTheme"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.speech.action.VOICE_SEARCH_RESULTS" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!--
For these schemes were not particular MIME type has been
supplied, we are a good candidate.
-->
<intent-filter>
<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="about" />
<data android:scheme="javascript" />
</intent-filter>
<!--
For these schemes where any of these particular MIME types
have been supplied, we are a good candidate.
-->
<intent-filter>
<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="inline" />
<data android:mimeType="text/html" />
<data android:mimeType="text/plain" />
<data android:mimeType="application/xhtml+xml" />
<data android:mimeType="application/vnd.wap.xhtml+xml" />
</intent-filter>
<!-- For viewing saved web archives. -->
<intent-filter>
<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="file" />
<data android:mimeType="application/x-webarchive-xml" />
</intent-filter>
<!-- Accept inbound NFC URLs at a low priority -->
<intent-filter android:priority="-101" >
<action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
<!-- We are also the main entry point of the browser. -->
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.APP_BROWSER" />
</intent-filter>
<!--
The maps app is a much better experience, so it's not
worth having this at all... especially for a demo!
<intent-filter android:label="Map In Browser">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/postal-address" />
</intent-filter>
-->
<intent-filter>
<action android:name="android.intent.action.WEB_SEARCH" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.WEB_SEARCH" /> <category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_SEARCH" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> <meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>

参考:http://www.apkbus.com/forum.php?mod=viewthread&tid=225694&page=1#pid3159981

设置默认Browser的更多相关文章

  1. [saiku] 简化/汉化/设置默认页

    上一篇分析了schema文件 [ http://www.cnblogs.com/avivaye/p/4877832.html] 在安装完毕Saiku后,由于是社区版本,所以界面上存在很多升级为商业版的 ...

  2. Ueditor设置默认字体、字号、行间距,添加字体种类(转)

    Ueditor默认字体.字号.行间距的修改: ueditor默认字号是16号,默认字体为sans-serif,默认行间距为5px,如下图所示: 首先,修改ueditor.all.js文件中如上图红框中 ...

  3. datepickerx设置默认日期

    datepicher插件是jQuery UI的一个插件,它提供一个日期弹出窗口(或直接显示在页面),供用户选择日期.在Web开发中,总会遇到需要用户输入日期的情况.一般都是提供一个text类型的inp ...

  4. .NET DateTime类型变量作为参数时设置默认值

    一个小的 Tips. .NET 中函数参数的默认值需要是编译时常量.如果参数是引用类型,可以设置Null,如果是值类型,可以设置相应的编译时常量,如整型可以用整数,但对于DateTime(结构体,值类 ...

  5. ng-option指令使用记录,设置默认值需要注意

    ng-options一般有以下用法: 数组作为数据源: label for value in array select as label for value in array label group ...

  6. IIS设置默认主页无效

    服务器系统:Windows server 2008 R2 IIS版本:7.5 IIS中部署一个dotnet framework 3.5的网站应用程序,设置"默认文档"为:index ...

  7. 《Entity Framework 6 Recipes》中文翻译系列 (14) -----第三章 查询之查询中设置默认值和存储过程返回多结果集

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-6在查询中设置默认值 问题 你有这样一个用例,当查询返回null值时,给相应属性 ...

  8. EF Core1.0 CodeFirst为Modell设置默认值!

    当我们使用CodeFirst时,有时候需要设置默认值! 如下 ; public string AdminName {get; set;} = "admin"; public boo ...

  9. cefsharp设置默认语言

    cefsharp是不错的浏览器内核封装版本之一,默认语言是en-US,这个一直困扰着项目,项目好多处需修改,后来经多次尝试,才发现,原来设置默认语言这么简单. CefSharp.Settings se ...

随机推荐

  1. HTTPS加密过程分析

    HTTPS加密的作目的是解决信息传输过程中数据被篡改,窃取 HTTPS使用了一系列的加密算法:对称加密算法.非对称加密算法.单向加密算法 非对称加密算法 分为公钥部分和私钥部分,用公钥加密的密文只能由 ...

  2. day22 面向对象基础

    1.什么是面向过程 在介绍面向对象之前,要先明确面向过程 在这之前我们所写的任何代码都是面向过程的 什么是面向过程? 是一种编程思想 面对 朝向 在编写代码时,要时刻想着过程这两个字 过程指的是什么? ...

  3. oracle 调用包体的函数并返回return值

    /// <summary> /// 执行数据库包体操作,返回结果 /// </summary> /// <param name="cmdText"&g ...

  4. 七 shelve模块

    shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型 import shelve f=shelve.o ...

  5. 搭建Java后台

    jdk+eclipse+svn+maven+mysql+tomcat7.0+sublime安装包和jar插件 配置管理工具-SVN http://download.csdn.net/detail/u0 ...

  6. 准备面试-DFT

    问题:面试DFT岗位的准备工作 1.在EETOP上搜索DFT看到的一些要求 1.要弄明白DCSCAN.ACSCAN.MBIST.边扫等原理, 2.要会利用相应的Synopsys或Mentor公司工具! ...

  7. Python os.getcwd()

    Python os.getcwd() 方法  Python OS 文件/目录方法 概述 os.getcwd() 方法用于返回当前工作目录. 语法 getcwd()方法语法格式如下: os.getcwd ...

  8. centos6与centos7区别

    CentOS 6 vs CentOS 7的不同   (1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME Shell) (2)文件系统[CentOS6 ...

  9. 207. Course Schedule(Graph; BFS)

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  10. 测试用户体验相关——UI设计准则及方法

    之前跟我们uxc同学聊过一些,记录下来,方便在工作中不断渗透深入和理解,能够逐渐养成比较好的审美和对UI交互问题的敏锐的觉察力. 以问题为导向来吧... 第一个问题:一个menu中的图标一定要风格一致 ...