Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236931
  • 博文数量: 61
  • 博客积分: 125
  • 博客等级: 入伍新兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-23 23:28
文章分类

全部博文(61)

文章存档

2014年(37)

2013年(21)

2012年(3)

分类: Android平台

2014-08-11 22:37:54

参考:http://blog.csdn.net/pediyasm/article/details/7987389
1.设置默认应用的原理
   当设置了默认应用后,本文以UC浏览器为例子,在手机/data/system/packages.xml中就会新增如下信息:

点击(此处)折叠或打开

  1. <preferred-activities>
  2. <item name="com.UCMobile/.main.UCMobile" match="200000" set="2">
  3. <set name="com.UCMobile/.main.UCMobile" />
  4. <set name="com.android.browser/.BrowserActivity" />
  5. <filter>
  6. <action name="android.intent.action.VIEW" />
  7. <cat name="android.intent.category.DEFAULT" />
  8. <scheme name="http" />
  9. </filter>
  10. </item>
  11. </preferred-activities>


2.实现方法。有两种方法可以实现:一种是在Provision.apk中实现,一种是在PackageManagerService.java中实现。具体代码如下:

点击(此处)折叠或打开

  1. private void addPreferredActivityForULauncher() {
  2.         IntentFilter filter = new IntentFilter();
  3.         filter.addAction("android.intent.action.VIEW");
  4.         filter.addCategory("android.intent.category.DEFAULT");
  5.      filter.addDataScheme("http");
  6.     
  7.         ComponentName preActivity = new ComponentName("com.UCMobile", "com.UCMobile.main.UCMobile");
  8.         ComponentName[] set = new ComponentName[] {preActivity,new ComponentName("com.android.browser", "com.android.browser.BrowserActivity")};
  9.         mSettings.mPreferredActivities.addFilter(
  10.                 new PreferredActivity(filter, 0x200000, set, preActivity));
  11.         mSettings.writeLPr();
  12.     }

点击(此处)折叠或打开

  1. if(isFirstBoot()){
  2.     addPreferredActivityForULauncher();
  3.      }
3.通过mm命令生成services.jar,并push到手机/system/framework下。同时将uc浏览器预装进系统。恢复出厂设置即可生效。
=========================
=========================
另外一种方法,在PackageManagerService.java添加如下代码:
在构造函数结尾添加setDefaultBrowser();

点击(此处)折叠或打开

  1. private final void setDefaultBrowser(){
  2.          Log.d("debug_default","setDefaultBrowser is called.");
  3.          if(!isFirstBoot()) {
  4.               Log.d("debug_default","not first boot, return");
  5.               return;
  6.          }
  7.          String str1 = "android.intent.category.DEFAULT";
  8.          String str2 = "android.intent.category.BROWSABLE";
  9.          String str3 = "android.intent.action.VIEW";
  10.          //intent filter
  11.          IntentFilter filter = new IntentFilter(str3);
  12.          filter.addCategory(str1);
  13.          filter.addCategory(str2);
  14.          filter.addDataScheme("http");
  15.          //intent
  16.          Intent intent = new Intent(str3);
  17.          intent.addCategory(str2);
  18.          intent.addCategory(str1);
  19.          Uri uri = Uri.parse("http://");
  20.          intent.setDataAndType(uri, null);
  21.          //set UC,if your want set other browser as default, modify below code, add package name and activiy name
  22.          ComponentName component = new ComponentName("com.UCMobile","com.UCMobile.main.UCMobile");
  23.          //find all browsers
  24.         List<ResolveInfo> resolveInfoList = queryIntentActivities(intent,intent.getType(),PackageManager.GET_INTENT_FILTERS, 0);
  25.         int size = resolveInfoList.size();
  26.         ComponentName[] arrayOfComponentName = new ComponentName[size];
  27.         //make sure the default browser in phone
  28.         boolean no_UC = true;
  29.         for (int i = 0; i < size; i++){
  30.             ActivityInfo activityInfo = resolveInfoList.get(i).activityInfo;
  31.             String packageName = activityInfo.packageName;
  32.             String className = activityInfo.name;
  33.             //if your want set other browser as default, modify below package name
  34.             if (packageName.equals("com.UCMobile")) no_UC = false;
  35.             ComponentName componentName = new ComponentName(packageName, className);
  36.             arrayOfComponentName[i] = componentName;
  37.         }
  38.         if (no_UC){
  39.             Log.d("debug_default","no uc package");
  40.             return;
  41.         }else{
  42.             Log.d("debug_default","uc package is in, set it as Preferred.");
  43.         }
  44.         //set default
  45.         addPreferredActivity2(filter,IntentFilter.MATCH_CATEGORY_SCHEME, arrayOfComponentName,component);
  46.         Log.d("debug_default","add UC to PreferredActivity");
  47.     }


  48.     public void addPreferredActivity2(IntentFilter filter, int match,ComponentName[] set, ComponentName activity) {
  49.         Log.d("debug_default","addPreferredActivity2 is called.");
  50.         // writer
  51.         synchronized (mPackages) {
  52.         Slog.i(TAG, "Adding preferred activity " + activity + ":");
  53.         filter.dump(new LogPrinter(Log.INFO, TAG), " ");
  54.         mSettings.editPreferredActivitiesLPw(0).addFilter(new PreferredActivity(filter, match, set, activity,true));
  55.         mSettings.writePackageRestrictionsLPr(0);
  56.         }
  57.     }

阅读(4082) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~