Android adaptation to a variety of ROM shortcuts

Source: Internet
Author: User

Shortcuts should say a lot of people have done, we will see how the basic shortcuts are implemented, what will be the problem?

Be sure to get permission first:

1  <!--Add Shortcut -2     <uses-permissionAndroid:name= "Com.android.launcher.permission.INSTALL_SHORTCUT" />3     <!--Remove Shortcut -4     <uses-permissionAndroid:name= "Com.android.launcher.permission.UNINSTALL_SHORTCUT" />5     <!--Query Shortcut -6     <uses-permissionAndroid:name= "Com.android.launcher.permission.READ_SETTINGS" />

Then define the action we want to initiate:

1  // action to delete a shortcut 2   Public Static Final String action_remove_shortcut = "Com.android.launcher.action.UNINSTALL_SHORTCUT"; 3  // action to add a shortcut 4   Public Static Final String action_add_shortcut = "Com.android.launcher.action.INSTALL_SHORTCUT";

Then write 2 methods:

1  //Delete Shortcut2    Public Static voidRemoveshortcut (Context context, Intent actionintent, String name) {3Intent Intent =NewIntent (action_remove_shortcut);4 Intent.putextra (Intent.extra_shortcut_name, NAME);5Intent.putextra ("Duplicate",false);6 Intent.putextra (intent.extra_shortcut_intent, actionintent);7 Context.sendbroadcast (intent);8  }9 //Add ShortcutTen  Public Static voidAddshortcut (Context context, Intent actionintent, String name, One                                    Booleanallowrepeat, Bitmap iconbitmap) { AIntent addshortcutintent =NewIntent (action_add_shortcut); -         //whether to allow duplicate creation -Addshortcutintent.putextra ("Duplicate", allowrepeat); the         //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); A}

This way, most of the phones can be used normally. But there are all sorts of problems on some homemade phones. For example, HW's mobile phone does not work at all.

Xiaomi's phone cannot be repeatedly created 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 we can create shortcuts in addition to sending broadcasts, and we should be able to create shortcuts by directly manipulating the database.

But we have to pay attention to, all the lanucher are very different, I used the simulator, so it is the official ROM, the path must be regular, but the deep customization of the Android system

We all know that the lanucher are doing their own, so favorites the position of the table in the end where, this is a problem. We need to find out where this table is in order to manipulate the table.

To find out where this table is located, we first need to know which lanucher is the starting function for this ROM? What is the package name?

1 //This function returns the package name of the Lanucher under the current ROM2     PrivateString Getcurrentlanucherpackagename (context context)3     {4         //This intent is good to understand is to start Lanucher intent5Intent intent=NewIntent (intent.action_main);6 intent.addcategory (intent.category_home);7         //Getpackagemanager (). resolveactivity This function is to query whether there are eligible activity8ResolveInfo Res=context.getpackagemanager (). Resolveactivity (intent,0);9         //To avoid null pointers we have to judge the empty, although you and I both know that this situation will not happenTen         if(res==NULL|| res.activityinfo==NULL) One         { A             return""; -         } -         returnRes.activityInfo.packageName; the}

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 ~ ~

Good to 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.

1 //This function returns the provider of the permission to find authority2     Privatestring Getauthorityfrompermission (context context, String permission) {3         //returns information about the provider of the installed app4list<packageinfo> packs =Context.getpackagemanager (). Getinstalledpackages (packagemanager.get_providers);5         //traverse the information obtained from the installation package6          for(PackageInfo pack:packs) {7             //The provider provided by each installation package are in this array8providerinfo[] providers =pack.providers;9             if(Providers! =NULL) {Ten                 //traverse each provider to see if the required permissions are equal to the permission parameters we passed in One                  for(Providerinfo providerinfo:providers) { A                     if(Permission.equals (providerinfo.readpermission) | |permission.equals (providerinfo.writepermission)) { -                         returnproviderinfo.authority; -                     } the                 } -             } -         } -         return""; +}
1   Private String Getauthorityfrompermissiondefault (context context) {2         return getauthorityfrompermission (context, "Com.android.launcher.permission.READ_SETTINGS"); 3     }
1  PrivateUri Geturifromlauncher (context context) {2StringBuilder Uristrbuilder =NewStringBuilder ();3         //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 Lanucher4String authority =Getauthorityfrompermissiondefault (context);5         //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.6         if(authority = =NULL|| Authority.trim (). Equals ("")) {7authority = getauthorityfrompermission (Context,getcurrentlanucherpackagename (context) + ". Permission. Read_settings ");8         }9Uristrbuilder.append ("content://");Ten         //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. One         //most of them are the logic inside of else. A         if(textutils.isempty (authority)) { -             intSdkint =Android.os.Build.VERSION.SDK_INT; -             if(Sdkint < 8) {//Android 2.1.x (API 7) and the following theUristrbuilder.append ("Com.android.launcher.settings"); -}Else if(Sdkint < 19) {//Android 4.4 or less -Uristrbuilder.append ("Com.android.launcher2.settings"); -}Else{//4.4 and above +Uristrbuilder.append ("Com.android.launcher3.settings"); -             } +}Else { A uristrbuilder.append (authority); at         } -Uristrbuilder.append ("/favorites?notify=true"); -         returnUri.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.

That's where the problem is basically solved, all of your shortcuts can be done directly through this URI, 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, and then find out the permissions he needs provider, and then in your manifest directly add permissions should be able to use the normal. Third party lanucher caused by the bug basically can be through this program

To solve.

Android adaptation to a variety of ROM shortcuts

Related Article

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.