Java 如何在安装时在Android手机的主屏幕上创建应用程序快捷方式

Java 如何在安装时在Android手机的主屏幕上创建应用程序快捷方式,java,android,Java,Android,我已经创建了一个android应用程序,它运行良好 但是现在我想添加一个功能,在安装时应用程序应该在主屏幕上创建一个快捷方式 请建议 我不想要完整的代码工作 简单的步骤就足够了首先声明您的应用程序正在使用AndroidManifest.xml中的INSTALL_快捷方式权限 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> Intent shor

我已经创建了一个android应用程序,它运行良好 但是现在我想添加一个功能,在安装时应用程序应该在主屏幕上创建一个快捷方式

请建议 我不想要完整的代码工作
简单的步骤就足够了

首先声明您的应用程序正在使用AndroidManifest.xml中的INSTALL_快捷方式权限

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



    Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra("someParameter", "HelloWorld");

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));

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

首先声明应用程序正在使用AndroidManifest.xml中的INSTALL_快捷方式权限

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



    Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra("someParameter", "HelloWorld");

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));

    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    context.sendBroadcast(addIntent);
在你的舱单上

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>
在你的舱单上

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>
试一试:

Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.setClassName(this, this.getClass().getName());

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
getApplicationContext().sendBroadcast(intent);
祝你好运…

试试看:

Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.setClassName(this, this.getClass().getName());

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
getApplicationContext().sendBroadcast(intent);

祝你好运…

此cod部分进入发射器活动??此cod部分进入发射器活动??