Wednesday, 29 January 2014

Android Add and Remove Application Shortcut in Home Screen Example

One of my friend is asking
When i am installing any application from the android market .I seen shortcut of the application in my mobile home screen.

how to create programmatically can u know ?

Based on that request Here i was creating one example how to create shortcut and remove shortcut on home screen.

Android provide intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to home screen.

intent class com.android.launcher.permission.UNINSTALL_SHORTCUT which can be used to remove shortcut from home screen.

Here i created Activity with two buttons one for Add shortcut and other one for remove shortcut.And i was using preferences to store state of shortcut (already created or not)

Android Activity(MainActivity.java)

package com.androidsurya.shortcuts;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Context mContext = MainActivity.this;
SharedPreferences appPreferences;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Preferences to store all ready created or not finding purpose.
appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
// Add listener to add shortcut button
Button add = (Button) findViewById(R.id.AddShortcut);
add.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (!(appPreferences.getBoolean("isShortcutCreated", false))) {
addShortcut(); // Add shortcut on Home screen

}

}
});

// Add listener to remove shortcut button
Button remove = (Button) findViewById(R.id.RemoveShortcut);
remove.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
removeShortcut(); // Remove shortcut from Home screen
}
});
}

private void addShortcut() {
// Adding shortcut for MainActivity
// on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Example");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isShortcutCreated", true);
editor.commit();
}

private void removeShortcut() {
// Deleting shortcut for MainActivity
// on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Example");
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isShortcutCreated", false);
editor.commit();
}

}

Android UI layout (activity_main.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/AddShortcut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add Shortcut" />

    <Button
        android:id="@+id/RemoveShortcut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Remove Shortcut" />

</LinearLayout>

Note: 
 Don't Forget Register this Below permissions  In Manifest File 

<uses-permission
         android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission
         android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

Output Screenshot:






   































Tags : Android application,app shortcut in homescreen,android homescreen with application shortcut,application shortcut,remove shortcut from home screen,how to add shortcut of application in home screen.

No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...