Android launcher development (4) solutions to automatically add Desktop shortcuts and common problems in actual development

Source: Internet
Author: User

Recently, the application just needs to add shortcuts. After referring to the source code and some other information on the Internet, it is done.

There were two problems at the same time,

1. After the program is uninstalled, the desktop shortcut still exists:

For this question, the old or current version of many online applications still exists. after referring to the source code, I found a solution: when creating the export cut, you need to set the extre_shortcut_intent action. and category to bind the created shortcut to your application:

2. Check whether the shortcut exists and the information of the shortcut. In the system application launcher, provide data in the form of contentprovider.

You need to read the shortcut information from the favorites table of launcher. dB of COM. Android. launcher.

For the Authority attribute in the lancher provider node, it is changed to "com. Android. launcher2.settings" in the android attribute, and the version earlier than 2.2 is "com. Android. launcher. Settings"

If the application needs to be compatible with machines earlier than 2.2, You need to obtain the current mobile phone system version number and change the authority in the URI.

After the above two problems are solved, the function will be improved. Friends who need it can be directly added to the application to be launched.

Effect;

The Code is as follows and detailed annotations have been added:

1. androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. android. autolancher "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "/> <uses-Permission Android: name = "com. android. launcher. permission. install_shortcut "/> <uses-Permission Android: Name =" com. android. launcher. permission. uninstall_shortcut "/> <! -- The shortcut information needs to be read from setting --> <uses-Permission Android: Name = "com. android. launcher. permission. read_settings "/> <application Android: icon =" @ drawable/ic_launcher "Android: Label =" @ string/app_name "> <activity Android: Name = ". launcherdemo2activity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter>
<! -If you want to enable the application to appear in the "Long press menu" and select "add shortcut", add the following code-> <intent-filter>

<Action Android: Name = "android. Intent. Action. create_shortcut"> </Action> </intent-filter> </activity> </Application> </manifest>

2. launcherdemo2activity:

Package COM. android. autolancher; import android. app. activity; import android. app. alertdialog; import android. app. alertdialog. builder; import android. content. contentresolver; import android. content. dialoginterface; import android. content. dialoginterface. onclicklistener; import android. content. intent; import android. database. cursor; import android.net. uri; import android. OS. bundle; public class launcherdemo2act Ivity extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Boolean flag = isshortcutinstalled (); // if it has been created, you do not need to create if (flag = false) {alertdialog. builder = new Builder (launcherdemo2activity. this); builder. settitle ("whether to create a desktop shortcut for this application"); builder. setpositivebutton ("yes", new onclicklistener () {public void onclick (Dialog Interface dialog, int which) {addshortcuttodesktop () ;}}); builder. setnegativebutton ("no", new onclicklistener () {public void onclick (dialoginterface dialog, int which) {}}); builder. create (). show () ;}} private void addshortcuttodesktop () {intent shortcut = new intent ("com. android. launcher. action. install_shortcut "); // reconstruction of shortcut is not allowed. putextra ("DUPLICATE", false); // you can specify the value of the parameter cut. putextra (I Ntent. extra_shortcut_name, "Garfield shortcut"); // you can specify the shortcut icon. putextra (intent. extra_shortcut_icon_resource, intent. using cuticonresource. fromcontext (this, R. drawable. CAT); // sets intent = new intent (this, this. getclass (); // The desktop icon is bound to the application. After the application is uninstalled, the system automatically deletes the icon intent. setaction ("android. intent. action. main "); intent. addcategory ("android. intent. category. launcher "); shortcut cut. putextra (intent. extra_shor Tcut_intent, intent); // send broadcast sendbroadcast (route cut);}/*** the shortcut information is saved on com. android. launcher. in the favorites table of DB, ** @ return */Public Boolean isshortcutinstalled () {Boolean isinstallshortcut = false; Final contentresolver Cr = launcherdemo2activity. this. getcontentresolver (); // 2.2 The system is "com. android. launcher2.settings ". For more information, see" com. android. launcher. settings "string authority = NULL;/** based on the version number Set the URI authority */If (getsystemversion ()> = 8) {authority = "com. android. launcher2.settings ";} else {authority =" com. android. launcher. settings ";} uri content_uri = Uri. parse ("content: //" + authority + "/favorites? Required y = true "); cursor c = Cr. Query (content_uri, new string [] {" title "," iconresource "}," Title =? ", New string [] {getstring (R. String. app_name)}, null); // XXX indicates the application name. If (C! = NULL & C. getcount ()> 0) {isinstallshortcut = true; system. out. println ("created");} return isinstallshortcut;}/*** get the system SDK version * @ return */private int getsystemversion () {return build. version. sdk_int ;}}

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.