0

I am trying to pass data with the shortcut that is created when the user clicks on a button. So that when the user runs the application through that specific shortcut the MainActivity receivers that data and perform decision on that data. For example, I want to pass the URL of the website. If the user clicks Button 1 the URL for that shortcut will be "http://expample1.com" and if Button 2 is clicked a new shortcut will be created with second URL "http://expample2.com".

I looked through other question but they only about creating a shortcut. Another method I came across is using ShortcutManager but it only supports API level 25 above. This is the code to create a shortcut that I wrote. Now how can I add data in it for use?

public void onBtnSecondShortCutCreate(View view) {
    Intent shortcutIntent = new Intent(getApplicationContext(), SecondActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    Intent addIntent = new Intent();
    addIntent.putExtra("SecondMessage","Second Activity");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "beSpider Second app");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.logo));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}
  • check this https://stackoverflow.com/questions/40446733/how-to-create-dynamic-app-shortcut-using-shortcutmanager-api-for-android-7-1-app – giveJob Jul 10 '18 at 11:57
  • The question you provided is using ShortcutManager whose minimum support is for API Level 25 while My application minimum API Level is 15. – Muhammad Abdullah Jul 10 '18 at 17:10

1 Answers1

0

Well, this API was added in API Level 1. Giving you an example of usage:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="exploitrme.shortcuttest">

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

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.class

package exploitrme.shortcuttest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (getIntent().getIntExtra("toe", 501) != 1123) {
            addShortcut();
        }else{
            Toast.makeText(getApplicationContext(),"Started From Shortcut",Toast.LENGTH_LONG).show();
        }
    }

    void addShortcut() {
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        shortcutIntent.putExtra("toe", 1123);

        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "SAMPLE SHORTCUT!!");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_ac_unit_black_24dp));
        addIntent.putExtra("duplicate", false);
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);

    }
}

[EDIT]: Updated with a sample application I just created, because the previous one answer wasn't clear for you.

exploitr
  • 843
  • 1
  • 14
  • 27
  • This does not work. It's not creating the icon. I updated the question and added the code that I wrote with help of another question. Please now help me how to add data into it. – Muhammad Abdullah Jul 10 '18 at 17:07
  • Really? Please check this : https://www.youtube.com/watch?v=_JbEHQQVmrs – exploitr Jul 11 '18 at 04:45
  • I've updated the code with the sample application I just created and I JUST TESTED it on Android 4.4, check the video, it works fine there. – exploitr Jul 11 '18 at 04:49
  • You just copy and paste the above code and test first if it works. It's a universal process and must work on a Kitkat device unless you've played with it's OS – exploitr Jul 11 '18 at 04:50
  • I'm here for that, no thanks. For any help free feel to ask again – exploitr Jul 11 '18 at 11:08