【问题标题】:Can I automatically create an app home shortcut when installing an app without launching?我可以在安装应用程序而不启动时自动创建应用程序主页快捷方式吗?
【发布时间】:2016-09-19 14:37:12
【问题描述】:

我刚刚在谷歌商店下载了一个应用程序并没有运行它。下载进度完成后,它会自动在我的主屏幕上创建一个应用程序主页快捷方式。谷歌商店上的每个应用程序都会自动执行此操作?如果不?我怎样才能做到这一点? 我可以在第一次启动应用程序时创建一个应用程序主页快捷方式:

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();
}

但是我想在安装应用程序而不启动时创建一个应用程序主页快捷方式,因为有时用户不会立即在谷歌商店打开我的应用程序,他们会在下载进度完成后找到应用程序主页快捷方式来打开它。 我还有另一个问题。当第一次使用该代码启动应用程序时,我的主屏幕快捷方式的应用程序标题可能会随着手机语言的变化而改变。但我希望它立即更改而不启动。 我的问题有解决方案吗?提前致谢。

【问题讨论】:

    标签: java android homescreen


    【解决方案1】:

    但我想在安装应用而不启动时创建应用主页快捷方式

    这通常是不可能的。在用户启动您的启动器活动或其他使用显式 Intent 启动您的组件之一之前,您的任何代码都不会运行。

    【讨论】:

    • 我刚刚在谷歌商店下载了一个应用程序并没有运行它。下载进度完成后,它会自动在我的主屏幕上创建一个应用程序主页快捷方式。谷歌商店中的每个应用都会自动执行此操作?
    • @KhoaMadridista:这取决于主屏幕的实现。
    • 我该怎么做? @CommonsWare
    • @KhoaMadridista:你怎么能做什么?写一个主屏幕?
    • google store 上的每个应用程序在下载完成后都会在主屏幕自动创建一个应用程序主页快捷方式,这样我就不需要做所有事情了?
    猜你喜欢
    • 2011-11-23
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多