Khoa Madridista
Khoa Madridista

Reputation: 23

Can I automatically create an app home shortcut when installing an app without launching?

I have just downloaded an app on google store and haven't run it. It automatically creates an app home shortcut at my home screen when downloading progress finishes. Every app on google store automatically do that ? If not? How can I do that? I can create an app home shortcut when launching app at first time with this method:

public void createOrUpdateShortcut() {

appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled = appPreferences.getBoolean("isAppInstalled", false);

String currentLanguage = Locale.getDefault().getDisplayLanguage();
String previousSetLanguage = appPreferences.getString("phoneLanguage", Locale.getDefault().getDisplayLanguage());

if (!previousSetLanguage.equals(currentLanguage)) {
    shortcutReinstall = true;
}

 if(!isAppInstalled || shortcutReinstall){

    Intent HomeScreenShortCut= new Intent(getApplicationContext(),
            BrowserLauncherActivity.class);

    HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
    HomeScreenShortCut.putExtra("duplicate", false);

    if(shortcutReinstall) {
        Intent removeIntent = new Intent();
        removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
        String prevAppName = appPreferences.getString("appName", getString(R.string.app_name));
        removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, prevAppName);
        removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
        getApplicationContext().sendBroadcast(removeIntent);
    }

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    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);


    //Make preference true
    SharedPreferences.Editor editor = appPreferences.edit();
    editor.putBoolean("isAppInstalled", true);
    editor.putString("phoneLanguage", currentLanguage);
    editor.putString("appName", getString(R.string.app_name));
    editor.commit();
}

But I want to create a app home shortcut when installing app without launching because sometimes user don't open my app immediately at google store and they find the app home shortcut to open it after downloading progress finishes. And I have another issue. My application title for homescreen shortcut can change on phone language change when launching app at first time with that code. But I want it changes immediately without launching. Are there solutions for my problem? Thanks in advance.

Upvotes: 0

Views: 72

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

But I want to create a app home shortcut when installing app without launching

That is not possible in general. Nothing of your code will run until either the user launches your launcher activity or something else uses an explicit Intent to start one of your components.

Upvotes: 2

Related Questions