CodeSolution

Please wait...

In Android Studio Create application Shortcut and uninstall programmatically

In Android Studio Create application Shortcut  and uninstall programmatically

@Android  @ Android Studio  @ Editer   @ ShortCut  @ programmatically  @ application  @ Install  @ Uninstall  

Nowadays many android apps installs a shortcut on home screen when you install the app and run it for the first time. This is a nice strategy to engage user by compiling them to use your app. Most of times when a user install an app, the app is deep buried in list of apps making it almost difficult to discover. So its always a good idea to make a shortcut right on home screen.

 

Creating New Project

  • Create a new project in Android Studio from File ⇒ New Project. When selecting Empty Activity and proceed.

Choosing the Colors

  • Open colors.xml located under res ⇒ values and add the below color values.

colors.XML

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
</resources>
  •  Open Style.xml located under res ⇒ values and add the below values.
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
  • add permission INSTALL_SHORTCUT to android manifest xml.
<uses-permission 
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission 
        android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

 

  • Create four XML layouts named activity_main.xml  under res ⇒ layouts.
<?xml version="1.0" encoding="utf-8"?>
<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/buttonAddShortcut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add Shortcut" />

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

</LinearLayout>

 

  • Open MenuActivity.java and modify the code as below. 

 

package com.example.appshortcut;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Run time permission
        if (checkAndRequestPermissions()) {
            // carry on the normal flow, as the case of  permissions  granted.
        }

        Button add = (Button) findViewById(R.id.buttonAddShortcut);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                addShortcut(); //Add shortcut on Home screen
            }
        });

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

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

    private  boolean checkAndRequestPermissions() {
        int Install = ContextCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.INSTALL_SHORTCUT);

        int Uninstall = ContextCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.UNINSTALL_SHORTCUT);

        List<String> listPermissionsNeeded = new ArrayList<>();

        if (Install != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.INSTALL_SHORTCUT);
        }
        if (Uninstall != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.UNINSTALL_SHORTCUT);
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this,
                    listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                    REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }


    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, "Codesolution");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                        R.drawable.ic_launcher_background));

        addIntent
                .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }

    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, "Codesolution");

        addIntent
                .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }

}
  • After Finally open AndroidManifest.XML and add code. 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.appshortcut">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SecondActivity"></activity>

        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

Note : This Method work only android version lower than marshmallow.

Thankyou smiley

 

 

 

 

 

 

 

 

Comments 0

Reply