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