yang163_yang 2014-05-16 08:13 采纳率: 33.3%
浏览 2463

android 创建应用快捷方式

我在程序第一次启动时创建应用快捷方式,从网上搜到的方法:
//创建快捷方式的Intent
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//不允许重复创建
shortcutintent.putExtra("duplicate", false);
//需要现实的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, application.getString(R.string.app_name));
//快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(application.getApplicationContext(), R.drawable.launcher);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//点击快捷图片,运行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(application.getApplicationContext() ,
WelcomeActivity.class));
//发送广播。OK
application.sendBroadcast(shortcutintent);

确实可以创建一个快捷方式,但是有如下问题:
入口为WelcomeActivity, 如果从启动器界面打开应用,并跳到另外一个Activity。 点击快捷方式永远都是再次跳转到一个新的WelcomeActivity实例, 而不是恢复当前状态。
如何处理能够使点击快捷方式是恢复到当前的Activity界面,而不是实例化一个新的目标Activity ?

  • 写回答

1条回答

  • yang163_yang 2014-05-16 09:35
    关注

    已找到解决方案:
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    //不允许重复创建
    shortcutintent.putExtra("duplicate", false);
    //需要现实的名称
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, application.getString(R.string.app_name));
    //快捷图片
    Parcelable icon = Intent.ShortcutIconResource.fromContext(application.getApplicationContext(), R.drawable.launcher);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    //点击快捷图片,运行的程序主入口
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory("android.intent.category.LAUNCHER");
    intent.setComponent(new ComponentName(application.getPackageName(), WelcomeActivity.class.getName()));
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    //发送广播。OK
    application.sendBroadcast(shortcutintent);

    重点是addCategory("android.intent.category.LAUNCHER");

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!