Tuesday, July 7, 2015

android: How to create and remove shortcut for Android application?

How to create and remove shortcut programmatically?

To Create Shortcut
It is very simple to create shortcut programmatically, all you need to do is broadcast the intent with necessary information.
Below code snippet will create the shortcut.
1
2
3
4
5
6
7
8
9
10
11
12
Intent shortCutInt = new Intent(getApplicationContext(),
                MainActivity.class);
shortCutInt.setAction(Intent.ACTION_MAIN);
Intent addInt = new Intent();
addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutInt);
addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppShortcut");
addInt.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                R.drawable.ic_launcher));
addInt.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// Broadcast the created intent
getApplicationContext().sendBroadcast(addInt);
To Remove Shortcut
Removing shortcut is as simple as creating shortcut, all you need to do is broadcast the intent with necessary information.
Below code snippet will remove the shortcut created.
1
2
3
4
5
6
7
8
9
Intent shortcutInt = new Intent(getApplicationContext(),
                MainActivity.class);
shortcutInt.setAction(Intent.ACTION_MAIN);
Intent addInt = new Intent();
addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutInt);
addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppShortcut");
addInt.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
// Broadcast the created intent
getApplicationContext().sendBroadcast(addInt);
Okay, enough theory, let’s create application.
Step 1: Create Android Application Project
  • Create new android project [File >> New >> Android Application Project] with project name AndroidShortCutExample
  • Enter package name as ‘com.prgguru.example’ and activity name as ‘MainActivity’
  • Choose Minimum Required SDK, Target SDK and Compile with. Confused on choosing these options? Take a look at Minimum Required SDK – Target SDK – Compile With post.
  • Click Next button and finally click ‘Finish’ to create project
Step 2: Design screen
Open activity_main.xml and replace it with below code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.prgguru.exameple.MainActivity" >
 
 <Button
 android:id="@+id/buttonAddShortcut"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:onClick="createShortCut"
 android:text="Create Shortcut" />
 
 <Button
 android:id="@+id/buttonRemoveShortcut"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:onClick="removeShortCut"
 android:text="Remove Shortcut" />
 
</LinearLayout>
Step 3: MainActivity.java – Main Class
Open MainActivity.java and replace it with below code.
Each line of code is commented well, if you still have any doubt or question 0 Comments it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.prgguru.exameple;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
 
public class MainActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
  
 /**
 * Method gets invoked when 'Create Shortcut' button is clicked
 *
 * @param v
 */
 public void createShortCut(View v){
 Intent shortCutInt = new Intent(getApplicationContext(),
 MainActivity.class);
 shortCutInt.setAction(Intent.ACTION_MAIN);
 Intent addInt = new Intent();
 addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCutInt);
 addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppShortcut");
 addInt.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
 Intent.ShortcutIconResource.fromContext(getApplicationContext(),
 R.drawable.ic_launcher));
 // Set Install action in Intent
 addInt.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
 // Broadcast the created intent
 getApplicationContext().sendBroadcast(addInt);
 }
  
 /**
 * Method gets invoked when 'Remove Shortcut' button is clicked
 *
 * @param v
 */
 public void removeShortCut(View v){
 Intent shortcutInt = new Intent(getApplicationContext(),
 MainActivity.class);
 shortcutInt.setAction(Intent.ACTION_MAIN);
 Intent addInt = new Intent();
 addInt.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutInt);
 addInt.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyAppShortcut");
 // Set Uninstall action in Intent
 addInt.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
 // Broadcast the created intent
 getApplicationContext().sendBroadcast(addInt);
 }
}
Step 4: Add permission in AndroidManifest.xml
Add install and uninstall shortcut permissions as shown below:
1
2
3
4
5
6
 <!-- Add permission to install Shortcut -->
 <uses-permission
 android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
 <!-- Add permision to uninstall Shortcut -->
 <uses-permission
 android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

No comments:

Post a Comment