在Android中安装后无法在主屏幕上创建应用程序的快捷方式 [英] Unable to create application's shortcut on Home Screen upon installation in android

查看:412
本文介绍了在Android中安装后无法在主屏幕上创建应用程序的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在home screen上创建应用程序的快捷方式.

I want to create a shortcut of the application on home screen when it is installed.

我将此添加到了 AndroidManifest.xml

<activity
        android:name="com.example.test.ShortCutActivity"
        android:icon="@drawable/ic_launcher"
        android:label="@string/title_activity_short_cut" >

        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

</activity>

我在我的项目中还创建了一个使用ShortCutActivity名称的Actvity.

Also I have a Actvity created with ShortCutActivity name in my project.

ShortCutActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ShortcutIconResource icon =
            Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);

        Intent intent = new Intent();

        Intent launchIntent = new Intent(this,MainActivity.class);

        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortCut");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

        setResult(RESULT_OK, intent);
}

但是它没有创建任何快捷方式.我想念什么吗?

But it is not creating any shortcut. Am I missing something ?

推荐答案

在您的活动中使用此示例代码,该活动在AndroidManifest.xml中设置为LAUNCHER和MAIN

Use this sample code in your activity which is set as a LAUNCHER and MAIN in AndroidManifest.xml

private void createShortcut(){
     Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
     shortcutIntent.setAction(Intent.ACTION_MAIN);
     Intent intent = new Intent();
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutDemo");
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
     intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
     getApplicationContext().sendBroadcast(intent);
}

还要在您的AndroidManifest.xml中应用以下权限

Also apply following permissions in your AndroidManifest.xml

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

希望这对您有用

这篇关于在Android中安装后无法在主屏幕上创建应用程序的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆