前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Set the Preferred Application in Android

Set the Preferred Application in Android

作者头像
技术小黑屋
发布2018-09-04 16:31:58
6130
发布2018-09-04 16:31:58
举报
文章被收录于专栏:技术小黑屋技术小黑屋

A great application should provide a function to let users set it as the preferred or default application. In this post, I will make a description on PreferredApplication Before an application is about to be set as default, it has two possibilities. One is that there is already a default application and the other is no default application.

Detect the Default Application Exists or Not

This is really important. It determines what we should do next.

1 2 3 4 5 6 7

public void testGetDefaultActivity() { PackageManager pm = mContext.getPackageManager(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.google.com")); ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); Log.i(VIEW_LOG_TAG, "getDefaultActivity info = " + info + ";pkgName = " + info.activityInfo.packageName); }

Result before I set Maxthon Browser as the default one. If no default one is set, the system will call the com.android.internal.app.ResolverActivity

1

I/View ( 1145 ): View getDefaultActivity info = ResolveInfo{410e4868 com.android.internal.app.ResolverActivity p=0 o=0 m=0x0};pkgName = android

Result after I set Maxthon Browser as the default one. If the default one is set just call the default activity

1

I/View ( 1145 ): View getDefaultActivity info = ResolveInfo{410ae1e8 com.mx.browser.MxBrowserActivity p=0 o=0 m=0x208000};pkgName = com.mx.browser

So it is really clear that you could use the following code to judge.

1 2

//Use this to determine whether default application is set or not boolean defaultSet = !"android".equals(info.activityInfo.packageName);

If the default one is already set

Just remove the preferred settings of the default application. However you could not do this in the code due to the permission control. It’s generally done by users manually. What you could do is lead the user to the clear default page(installedAppDetails)

1 2 3 4 5

public void testStartAppDetails() { //Use the destination package name Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:com.mx.browser")); getActivity().startActivity(intent); }

Then you navigate the user to the installedAppDetails and you could show some tips to click Button Clear Default.

When users go back from installedAppDetails to your application check is there any default appliction now. If the default application(in general, it’s not the previous one) remains repeat the above actions until there is not default one.

No default One

Now there is only one step to set ours as the default one. You could not do this in codes. It’s also required manual operation. What you could do is popup the selecting dialog and tip the use select your one.

1 2 3 4 5 6 7 8

public void testStartChooseDialog() { Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.addCategory("android.intent.category.BROWSABLE"); intent.setData(Uri.parse("http://androidyue.github.io/")); intent.setComponent(new ComponentName("android","com.android.internal.app.ResolverActivity")); getActivity().startActivity(intent); }

The dialog is like this

What’s more, your tips is really essential. Make sure to notify that “Use by default for this action” should be checked.

How to Clear my own default

1 2 3 4

public void testClearDefault() { PackageManager pm = mContext.getPackageManager(); pm.clearPackagePreferredActivities(mContext.getPackageName()); }

Note you could only clear packagePreferredActivities of your own.

A step into system control

the following settings is some content of /data/system/packages.xml

1 2 3 4 5 6 7 8 9 10 11 12

<preferred-activities> <item name="com.mx.browser/.MxBrowserActivity" match="200000" set="2"> <set name="com.android.browser/.BrowserActivity" /> <set name="com.mx.browser/.MxBrowserActivity" /> <filter> <action name="android.intent.action.VIEW" /> <cat name="android.intent.category.BROWSABLE" /> <cat name="android.intent.category.DEFAULT" /> <scheme name="http" /> </filter> </item> </preferred-activities>

Special

  • What will system do if I install or uninstall a related application?
  • Actually the system provide you a special chance to rechoose if a related activity is added or removed. When you click the url it should popup a dialog not use the previous preferred activity.
  • When the system popup the choosing dialog?
  • After some tests I guess the system will make a list of interested activities instantly. And then use the previous list to compare with the list stored in /data/system/packages.xml. If they are not the same, the choose dialog should be poped up.

Others

  • Android Hacker’s Handbook
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Detect the Default Application Exists or Not
  • If the default one is already set
  • No default One
  • How to Clear my own default
  • A step into system control
  • Special
  • Others
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档