How is the shortcut to a variety of ROMs in Android development implemented?

Source: Internet
Author: User

In Android development, to improve the development efficiency, to master some shortcuts is essential, especially for the Android entry stage of children's shoes, it is very important. Today, in the Android Development tutorial Web site, a number of commonly used Android to adapt to a variety of ROM shortcuts, share to everyone, to see how they are realized.

First, you need to get permissions:

<!--Add a shortcut--

<uses-permission android:name= "Com.android.launcher.permission.INSTALL_SHORTCUT"/>

<!--Remove a shortcut--

<uses-permission android:name= "Com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

<!--query Shortcuts--

<uses-permission android:name= "Com.android.launcher.permission.READ_SETTINGS"/>

Then, define the action we want to initiate:

Action to delete a shortcut

public static final String action_remove_shortcut = "Com.android.launcher.action.UNINSTALL_SHORTCUT";

Action to add a shortcut

public static final String action_add_shortcut = "Com.android.launcher.action.INSTALL_SHORTCUT";

Then write 2 methods:

Delete Shortcut

public static void Removeshortcut (context context, Intent actionintent, String name) {

Intent Intent = new Intent (action_remove_shortcut);

Intent.putextra (Intent.extra_shortcut_name, NAME);

Intent.putextra ("duplicate", false);

Intent.putextra (Intent.extra_shortcut_intent, actionintent);

Context.sendbroadcast (Intent);

}

Add Shortcut

public static void Addshortcut (context context, Intent actionintent, String name,

Boolean allowrepeat, Bitmap iconbitmap) {

Intent addshortcutintent = new Intent (action_add_shortcut);

Whether to allow duplicate creation

Addshortcutintent.putextra ("Duplicate", allowrepeat);

Title of Shortcut

Addshortcutintent.putextra (Intent.extra_shortcut_name, NAME);

Icon for shortcut

Addshortcutintent.putextra (Intent.extra_shortcut_icon, Iconbitmap);

Action of Shortcut

Addshortcutintent.putextra (Intent.extra_shortcut_intent, actionintent);

Context.sendbroadcast (addshortcutintent);

}

This method, in most of the mobile phone can be used normally. But there are all sorts of problems on some homemade phones. For example, HW mobile phone does not work completely, Xiaomi's phone can not be repeated to create shortcuts and so on. Now let's see if there's any way to solve these problems.

First, let's look at a picture:

This picture is very clear to tell US launcher this application has a database.

We can open this database and see what it is?

You see this at a glance, shortcuts are present in this table! So, in addition to sending a broadcast can create shortcuts, we directly manipulate the database should also be able to create shortcuts.

But we have to pay attention to Ah, the lanucher are very different, the above is the use of the simulator, so it is the official ROM, that path is certainly the conventional, but the depth of the custom Android system, we all know that each family lanucher are doing, so favorites It's a question of where the table is located. We need to find out where this table is in order to operate the table, to know where the table is, we first need to know for this ROM, which lanucher in the start function? What is the package name?

This function returns the package name of the Lanucher under the current ROM

Private String Getcurrentlanucherpackagename (context context)

{

This intent is good to understand is to start lanucher intent

Intent intent=new Intent (Intent.action_main);

Intent.addcategory (Intent.category_home);

Getpackagemanager (). resolveactivity This function is to query whether there are eligible activity

ResolveInfo Res=context.getpackagemanager (). resolveactivity (intent,0);

To avoid null pointers we have to judge the empty, although you and I both know that this situation will not happen

if (res==null| | Res.activityinfo==null)

{

Return "";

}

return res.activityInfo.packageName;

}

Some people may not understand the 4-5 line, why this equivalent to start lanucher intent is actually very simple, when we run an app in Android studio, we usually see the following interface:

You see, Android Studio actually installs our app and then uses this intent to start the lanucher on the phone and let Lanucher run our app, because studio doesn't know which Lanucher is in your phone, So it can only be done with intent, which is why the above code is sure to run successfully! Otherwise, Android Studio won't be able to get your app run in.

Of course, you can also enter the shell, the PS command to see if it is not the process of the package name in the Run ~ ~

Get our Lanucher package name, the next step is to go to our package name operation that database can be. It is clear that this step is the most appropriate contentprovider to operate.

This function returns the provider of the permission to find authority

private string Getauthorityfrompermission (context context, String permission) {

Returns information about the provider of the installed app

list<packageinfo> packs = Context.getpackagemanager (). Getinstalledpackages (Packagemanager.get_providers);

Traverse the information obtained from the installation package

for (PackageInfo pack:packs) {

The provider provided by each installation package are in this array

providerinfo[] providers = pack.providers;

if (providers! = null) {

Traverse each provider to see if the required permissions are equal to the permission parameters we passed in

for (Providerinfo providerinfo:providers) {

if (permission.equals (providerinfo.readpermission) | | permission.equals (providerinfo.writepermission)) {

return providerinfo.authority;

}

}

}

}

Return "";

}

Private String Getauthorityfrompermissiondefault (context context) {

Return getauthorityfrompermission (Context, "Com.android.launcher.permission.READ_SETTINGS");

}

Private Uri Geturifromlauncher (context context) {

StringBuilder Uristrbuilder = new StringBuilder ();

For the sake of speed, here we look at the default to see if we can find out because most of the phone's ROM or the default Lanucher

String authority = getauthorityfrompermissiondefault (context);

If you can't find it, it means that the ROM must be a custom lanucher. So just spell this custom Lanucher permission and go find it again.

if (authority = = NULL | | Authority.trim (). Equals ("")) {

authority = getauthorityfrompermission (Context,getcurrentlanucherpackagename (context) + ". Permission. Read_settings ");

}

Uristrbuilder.append ("content://");

If you can't find the authority in the method above, the following method must have been found, but very few of them will be the following.

Most of them are the logic inside of else.

if (textutils.isempty (authority)) {

int sdkint = Android.os.Build.VERSION.SDK_INT;

if (Sdkint < 8) {//Android 2.1.x (API 7) and the following

Uristrbuilder.append ("com.android.launcher.settings");

} else if (Sdkint < +) {//Android 4.4 or less

Uristrbuilder.append ("com.android.launcher2.settings");

} else {//4.4 and above

Uristrbuilder.append ("com.android.launcher3.settings");

}

} else {

Uristrbuilder.append (authority);

}

Uristrbuilder.append ("/favorites?notify=true");

Return Uri.parse (Uristrbuilder.tostring ());

}

Well, here we can get all the shortcuts to any one of the ROM's phones. The URI for the database table.

Here should be a basic solution to the problem, you can all the shortcut to the operation of the URI directly through this, nothing more than a few crud assembly.

No longer need to go through the broadcast program, but it is important to note that this method is usually time-consuming, depending on the performance of the mobile phone 200ms-600ms to complete the URI lookup. So remember to do a bit of asynchronous processing.

And all of your needs for Lanucher can do so, for example, some features in the use of native Lanucher mobile phone use normal, in Xiaomi Huawei Oppo use is not normal, you go to find out the abnormal ROM Lanucher package name, Then find out what permissions he provider need, and then in your manifest directly add the permissions should be able to use the normal. A bug caused by a third-party lanucher can basically be solved by this solution.

The above is the Android to adapt to a variety of ROM shortcut implementation method, we might as well in their own Android environment to try to hurt the face of these operations, to see if there is any problem.

How is the shortcut to a variety of ROMs in Android development implemented?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.