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

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)