Android 快捷启动图标

Android 快捷启动图标

核心代码

Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//不允许重复创建
shortcut.putExtra("duplicate", false)
//需要实现的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, strName);
//快捷图片
Parcelable icon = Intent.ShortcutIconResource.formContext(context, iconResId);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_RESOURCE, icon);
//点击快捷图片,运行程序

Intent intent = new Intent(context.getApplicationContext(),MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//Android启动模式 清除当前activity main 及以上的 activity 并从新创建 MainActivity;
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

需要注意

被启动的 activity 必须是默认启动的ac

  <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

猜你喜欢

转载自blog.csdn.net/ff_hh/article/details/78331739