Android shortcuts 1-create shortcuts through applications

Source: Internet
Author: User

Android shortcuts are the most basic components on the desktop. It is used to directly start a component of an application.

In general, you can create a shortcut to change an application on the left side of the Launcher Application List by long-clicking the icon of an application. In addition, you can add shortcuts to the desktop in two ways:

1. Create an Intent in the application and notify Launcher to create a shortcut in the form of Broadcast.

2. register an IntentFilter that meets specific conditions for the application components, and then add a shortcut to start the component on the Launcher desktop.

The following describes how to add shortcuts to an application.

Main. xml layout File

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: id = "@ + id/createShortcut"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: textSize = "20px"
Android: text = "create shortcuts"/>
<Button android: id = "@ + id/exit"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: textSize = "20px"
Android: text = "quit"/>
</LinearLayout>

Configuration File

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. ljq. action" android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon"
Android: label = "@ string/app_name">
<Activity android: name = ". ShortCutAction"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category
Android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

</Application>
<Uses-sdk android: minSdkVersion = "7"/>
<! -- Add Shortcut Key Permissions -->
<Uses-permission
Android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/>
</Manifest>

ShortCutAction class

Package com. ljq. action;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. OS. Parcelable;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
/**
* Create shortcuts through applications
*
* @ Author jiqinlin
*
*/
Public class ShortCutAction extends Activity implements OnClickListener {
Private Button createShortcut = null; // create a shortcut
Private Button exit = null; // exit the system

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

CreateShortcut = (Button) findViewById (R. id. createShortcut );
Exit = (Button) findViewById (R. id. exit );
CreateShortcut. setOnClickListener (this );
Exit. setOnClickListener (this );
}

Public void onClick (View v ){
// Button btn = (Button) v;
Switch (v. getId ()){
Case R. id. createShortcut:
// String title = getResources (). getString (R. string. title );
Intent addIntent = new Intent ("com. android. launcher. action. INSTALL_SHORTCUT ");
Parcelable icon = Intent. javascuticonresource. fromContext (this, R.drawable.png); // get the shortcut key icon
Intent myIntent = new Intent (this, ShortCutAction. class );
AddIntent. putExtra (Intent. EXTRA_SHORTCUT_NAME, "shortcut"); // the title of the shortcut
AddIntent. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE, icon); // shortcut icon
AddIntent. putExtra (Intent. EXTRA_SHORTCUT_INTENT, myIntent); // shortcut action
SendBroadcast (addIntent); // send Broadcast
Break;
Case R. id. exit:
System. exit (0 );
Break;
}
}
}

Running result

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.