Android shortcut Development (I) determine whether there is a shortcut

Source: Internet
Author: User

There are several ways to create an application shortcut:

  • On the launcher Application List, long press an application icon to create a shortcut to the desktop
  • On the desktop, press the shortcut button in the pop-up box and select "shortcut"> "application"> "add a shortcut program ".
  • Automatically created when the program is running

In the development of shortcuts, you must first determine whether a shortcut exists: Generally, add the oncreate method in the welcome interface of the program and the oncreate method of the activity that opens the first interface of the program.

If (! Hasshortcut ()){
Toast. maketext (mcontext, "saved shortcuts", Toast. length_long). Show ();
} Else {
Toast. maketext (mcontext, "no shortcut", Toast. length_long). Show ();
}

Creation method:


/**
* Determine whether shortcuts have been added to the desktop
*
* @ Param CX
* @ Param titlename
* Shortcut name
* @ Return
*/
Public Boolean hasshortcut (){
Boolean result = false;
// Obtain the name of the current application
String title = NULL;
Try {
Final packagemanager PM = getpackagemanager ();
Title = PM. getapplicationlabel (
PM. getapplicationinfo (getpackagename (),
Packagemanager. get_meta_data). tostring ();
} Catch (exception e ){
}

Final string uristr;
If (Android. OS. Build. version. sdk_int <8 ){
Uristr = "content: // com. Android. launcher. Settings/favorites? Required y = true ";
} Else {
Uristr = "content: // com. Android. launcher2.settings/favorites? Required y = true ";
}
Final URI content_uri = URI. parse (uristr );
Final cursor c = getcontentresolver (). Query (content_uri, null,
"Title =? ", New string [] {Title}, null );
If (C! = NULL & C. getcount ()> 0 ){
Result = true;
}
Return result;
}

Finally, add permissions:

<Uses-Permission Android: Name = "com. Android. launcher. Permission. read_settings"

OK! Judgment complete!

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.