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");

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料