"Sail Plan 022" 2015 sail plan Android Apidemo The Devil Step App->launcher shortcuts to create a shortcut on home screen for a non-active activity

Source: Internet
Author: User
Tags home screen



Android operating system for <intent-filter> activity with the following properties will display an entry in Application Manager (Launcher), which typically corresponds to the main activity of an application.


<action android:name= "Android.intent.action.MAIN"/><category android:name= " Android.intent.category.LAUNCHER "/>

 


In addition, if the user wants to add an app shortcut to the device's home screen, you can press and hold the app's icon in launcher, and the Android system will automatically add a shortcut to the app on the home screen. The name and icon are the same as in launcher.



In addition to supporting shortcuts to apps (main activity), Android can define shortcuts to any activity in application on home screen.






For example, your app has a feature users may often use, such as the map to query the address, under normal circumstances users need to start the main activity, may need to go through a few menu choices or other ways to enter this function, the user may feel inconvenient, which is can for this function in the home Screen creates a shortcut that will be accessed directly by the user following the shortcut, even if the activity is not the main activity.



Launcher shortcuts describes how to create a shortcut on home screen for a non-active activity.



To implement this shortcut, you can do it in one of the following steps:



1. <intent-filter> add to activity that needs to create a shortcut

That identifies this activity to support the addition of shortcuts on home screen. Launcher Shortcuts is an alias that uses Activity-alias,activity-alias as target, similar to activity.



2. Add the code for the user to add the shortcut, generally in the activity's OnCreate method to install shortcuts for activity:


 
private static final String EXTRA_KEY = "com.example.android.apis.app.LauncherShortcuts";

 

        // If the intent is a request to create a shortcut, we‘ll do that and exit if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
            setupShortcut();
            finish(); return;
        }

 

    /** * This function creates a shortcut and returns it to the caller.  There are actually two 
     * intents that you will send back.
     * 
     * The first intent serves as a container for the shortcut and is returned to the launcher by 
     * setResult().  This intent must contain three fields:
     * 
     * <ul>
     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
     * the shortcut.</li>
     * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut‘s icon, if provided as a
     * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
     * a drawable resource.</li>
     * </ul>
     * 
     * If you use a simple drawable resource, note that you must wrapper it using
     * {@link android.content.Intent.ShortcutIconResource}, as shown below.  This is required so
     * that the launcher can access resources that are stored in your application‘s .apk file.  If 
     * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras 
     * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
     * 
     * The shortcut intent can be any intent that you wish the launcher to send, when the user 
     * clicks on the shortcut.  Typically this will be {@link android.content.Intent#ACTION_VIEW} 
     * with an appropriate Uri for your content, but any Intent will work here as long as it 
     * triggers the desired action within your Activity. */ private void setupShortcut() { // First, set up the shortcut intent.  For this example, we simply create an intent that // will bring us directly back to this activity.  A more typical implementation would use a // data Uri in order to display a more specific result, or a custom action in order to // launch a specific operation.  Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
        shortcutIntent.setClassName(this, this.getClass().getName());
        shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut"); // Then, set up the container intent (the response to the caller)  Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name));
        Parcelable iconResource = Intent.ShortcutIconResource.fromContext( this,  R.drawable.app_sample_code);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); // Now, return the result to the launcher  setResult(RESULT_OK, intent);
    }

 





If the user wants to create a shortcut for an activity by pressing the blank director in Home screen, Android will display a list of desktop categories that the user can choose to add, and after selecting the shortcut (shortcut), Android will list all All applications of the Android.intent.action.CREATE_SHORTCUT are justified:






At this point, if you select Apidemos, then add to Home screen launches Launchershortcuts Activity, and the ACTION of the requested intent is set to Intent.action_create_ SHORTCUT, so you can use Intent.ACTION_CREATE_SHORTCUT.equals (ACTION) in OnCreate to determine whether the request is from Add to Home screen or user selection app-> Launcher shorts. If it is from add to Home Screen,launcher Shortcuts Create a shortcut setupshortcut for this activity, and then exit.



ADD to Home screen emits intent (run Startactivityforresult), which is expected to return a result from launchershortcuts, which is why Setupshortcut needs to return a result. The INTENT that corresponds to the created shortcut needs to define at least three parameters: Shortcut_intent (value:intent), Shortcut_name (value:string), Shortcut_icon (value: BITMAP) or Shortcut_icon_resource (Value:shortcuticonresource).



This example creates a shortcut for the current activity (launchershortcuts), which can be used for other activity as needed or to provide a list for users to select the shortcuts to create.



This example creates a sample shortcut in Home screen, selects the shortcut, and then goes directly to launcher shortcuts without having to enter launcher shortcuts from the app.









"Sail Plan 022" 2015 sail plan Android Apidemo The Devil Step App->launcher shortcuts to create a shortcut on home screen for a non-active activity


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.