Android as a great system, naturally provides an implementation for setting the default open program. In this article, I'll explain how to set the default program in the Android system. Before you set up the default program, there are only two scenarios, one with the default program and the other without any default programs.
Detect if there is a default program
The inspection is necessary because the result is about what we should do next.
Copy Code code as follows:
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 + ";p kgname =" + Info.activityInfo.packageName);
}
Its corresponding log output is as follows
Copy Code code as follows:
I/view (1145): View getdefaultactivity info = resolveinfo{410e4868 com.android.internal.app.ResolverActivity p = o=0 m=0x0};p kgname = Android
If there is no default program, then will show the default will show Com.android.internal.app.ResolverActivity, then what is this resolveractivity, in fact, it is a choice to open the Program dialog box, That's the way it should be
If we set up a proud browser as our default browser, the default program should display information about the browser. As follows.
Copy Code code as follows:
I/view (1145): View getdefaultactivity info = resolveinfo{410ae1e8 com.mx.browser.MxBrowserActivity p = o=0 m=0x20800 0};p kgname = Com.mx.browser
So how to determine whether the default program is set, the above method default Resolveinfo, if Info.activityInfo.packageName for Android, there is no setting, otherwise, there is a default program.
Copy Code code as follows:
Public Final Boolean haspreferredapplication (final context, final Intent Intent) {
Packagemanager pm = Context.getpackagemanager ();
Resolveinfo info = pm.resolveactivity (intent, packagemanager.match_default_only);
Return! " Android ". Equals (Info.activityInfo.packageName);
}
If you have a default program
What we need to do is clear the default settings for the default program now.
We can rewrite the default settings directly into our code, in fact, because of the issue of permissions. The reason is that Android has a great user concept because Linux is based on Linux, and every application in Android is a user in the Linux kernel. Two users of the same level cannot delete each other.
So, we can only hand it over to the user. Of course, this is also the practice of most procedures. All you have to do is take your users to the program details page, and you can jump to the application details page by using this code.
Copy Code code as follows:
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);
}
When you jump to the installed Application Details page, you should be prompted to use the clear Default button.
If the user returns to your program from the installation Details page, you need to detect if the user has cleaned up the default program settings, judged by whether there is a default program setting, or if there is a default, continue to prompt you to manually clean up other programs that have been set up, until the user is completely cleaned up, Then follow the procedure below without the default settings.
Note that there are multiple cleanup situations, such as setting up a browser, and after clearing the UC default settings, you may need to clean up the Dolphin browser.
There is no default program.
If there is no default program, we will need to set the program we want as the default, but the page can not be implemented in the code, or the need for artificial interaction to choose. All you need to do is use code like the following, and then pop a hint, Tell the user to select your program as the default program. As for the tips you can give full play to your imagination.
Copy Code code as follows:
public void Teststartchoosedialog () {
Intent Intent = new Intent ();
Intent.setaction ("Android.intent.action.VIEW");
Intent.addcategory ("Android.intent.category.BROWSABLE");
Intent.setdata (Uri.parse ("http://jb51.net"));
Intent.setcomponent (New ComponentName ("Android", "com.android.internal.app.ResolverActivity"));
Getactivity (). StartActivity (Intent);
}
To cancel your default program settings
Copy Code code as follows:
public void Testcleardefault () {
Packagemanager pm = Mcontext.getpackagemanager ();
Pm.clearpackagepreferredactivities (Mcontext.getpackagename ());
}
The above methods can only clean up your default settings
A step closer
In fact, the configuration file about the default settings is stored in the/data/system/packages.xml
Copy Code code as follows:
<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>
One more Thing
Question: What does the system do when a program is installed or uninstalled?
Answer: When a program is installed or uninstalled, take the browser as an example, if you install a Web browser or uninstall a UC browser, when you open a link from an external program, the system will not use the previous default program to open, but pop-up a selection dialog box for you to choose from.
Question: When will the system pop up select open Programs list
Answer: By my test, actually, when a intent comes over, the system dynamically collects the activity that can be processed and then compares it from the/data/system/packages.xml read, and pops up the selection dialog box if the two are different.