Create a shortcut icon for your app

Source: Internet
Author: User

Permissions:

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

 Packagecom.example.t02;Importandroid.app.Activity;Importandroid.app.Fragment;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); if(Savedinstancestate = =NULL) {Getfragmentmanager (). BeginTransaction (). Add (R.id.container,Newplaceholderfragment ()). commit (); }    }    /*** A placeholder fragment containing a simple view. */     Public Static classPlaceholderfragmentextendsFragment {Button btnchange;  Public Static FinalString action_add_shortcut = "Com.android.launcher.action.INSTALL_SHORTCUT";  Publicplaceholderfragment () {} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View Rootview=inflater.inflate (R.layout.fragment_main, container,false); Btnchange=(Button) Rootview.findviewbyid (R.id.btnchange); Btnchange.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {addshortcut ("Tttt");                        }            }); returnRootview; }                Private voidaddshortcut (String name) {Intent addshortcutintent=NewIntent (action_add_shortcut); //do not allow duplicate creationAddshortcutintent.putextra ("Duplicate",false);//            //nameAddshortcutintent.putextra (Intent.extra_shortcut_name, NAME); //iconsAddshortcutintent.putextra (Intent.extra_shortcut_icon_resource, Intent.shortcuticonreso            Urce.fromcontext (Getactivity (), r.drawable.ic_launcher2)); //setting up associated activityIntent launcherintent =NewIntent (Intent.action_main); Launcherintent.setclass (Getactivity (), mainactivity.class);            Launcherintent.addcategory (Intent.category_launcher);            Addshortcutintent. PutExtra (Intent.extra_shortcut_intent, launcherintent); //Send broadcastgetactivity (). Sendbroadcast (addshortcutintent); }    }}

Create a shortcut icon for your app

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.