Create shortcuts for apps in Android and android shortcuts
(1) Use sending broadcast to create a shortcut: In this demo, the function is as follows: click a button on the interface to generate a shortcut, click the shortcut to go To the dialing page;
The generation process is as follows:
1: The following permissions: <uses-permission android: name = "com. android. launcher. permission. INSTALL_SHORTCUT"/>
2: Add Action to a new Intent in Activity:
_ Intent. setAction ("com. android. launcher. action. INSTALL_SHORTCUT ");
3. Other core codes are as follows:
Intent _ ReturnIntent = new Intent ();
// Sets the filter action for creating shortcuts
_ ReturnIntent
. SetAction ("com. android. launcher. action. INSTALL_SHORTCUT ");
// Set the name of the generated shortcut
_ ReturnIntent. putExtra (Intent. EXTRA_SHORTCUT_NAME,
"Broad ShortCut ");
// Set the generated shortcut icon
_ ReturnIntent. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE,
Intent. Reset cuticonresource. fromContext (
LauncherActivity. this, R. drawable. ic_launcher ));
Intent _ Intent = new Intent (Intent. ACTION_CALL );
_ Intent. setData (Uri. parse ("tel: // 5556 "));
_ ReturnIntent. putExtra (Intent. EXTRA_SHORTCUT_INTENT, _ Intent );
// Send broadcast generation shortcut
SendBroadcast (_ ReturnIntent );
LauncherActivity. this. finish ();
}
Of course, the above permission should be added to the call:
<Uses-permission android: name = "android. permission. CALL_PHONE"/>
If you want to uninstall shortcuts, you need to add permissions to the layout file.
<Uses-permission android: name = "com. android. launcher. permission. UNINSTALL_SHORTCUT"/>
Then, import com. android. launcher. permission. UNINSTALL_SHORTCUT in intent.
(2) Use an Activity, and then click Menu> Add> select shortcut> select the shortcut of the created application on the Home page. The following results are displayed:
The procedure is as follows:
①: Register the Activity in the Androidmanifset. xml file.
②: Add <action/>
Take a look at the core code in the Activity:
Public class ShortCutSample extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
If (getIntent (). getAction (). equals (
"Android. intent. action. CREATE_SHORTCUT ")){
Intent _ ReturnIntent = new Intent ();
// Set the shortcut name
_ ReturnIntent. putExtra (Intent. EXTRA_SHORTCUT_NAME,
"Jiangqq ShortCut ");
// Set the shortcut icon
_ ReturnIntent. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE,
Intent. Reset cuticonresource. fromContext (this,
R. drawable. ic_launcher ));
Intent _ Intent = new Intent (Intent. ACTION_CALL );
_ Intent. setData (Uri. parse ("tel: // 10086 "));
// After the shortcut is created, click the icon to go to the page for dialing.
_ ReturnIntent. putExtra (Intent. EXTRA_SHORTCUT_INTENT, new Intent (
This, LauncherActivity. class ));
// Set the return value, which is generally OK,
SetResult (RESULT_ OK, _ ReturnIntent );
Finish ();
}
}