两种方法:

方法一、

void setshortCut() {

   Intent addShortcut = new Intent();

// 设置快捷方式的名字

   addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式名");

// 构建快捷方式中专门的图标

 Parcelable icon = Intent.ShortcutIconResource.fromContext(ShortcutTest.this, R.drawable.icon); // 添加快捷方式图标

   addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

// 构建快捷方式执行的Intent

   Intent mailto = new Intent(this, ShortcutTest.class);

// 添加快捷方式Intent

   addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mailto);

// 正常

   setResult(RESULT_OK, addShortcut);

}

方法二、

private void addShortcut(){

   Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

//快捷方式的名称

   shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

shortcut.putExtra("duplicate", false); //不允许重复创建

   //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer

   //注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序

 ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());

   shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

  shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this,ShortcutTest.class)); //快捷方式的图标

  ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

   shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

  sendBroadcast(shortcut);

}

方法二,直接添加到桌面上,但是此需要添加下面的权限:

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

更多相关文章

  1. Android(安卓)Studio 编译慢解决方法
  2. 【Android(安卓)开发教程】Fragment的生命周期
  3. Ubuntu中编译Android(安卓)JellyBean 4.2.1源码出现Switch.pm出
  4. Android(安卓)Studio 快捷键使用
  5. Android中用GridView实现九宫格的两种方法
  6. Android(安卓)SDK 1.5 "--core-library" build error问题解决方
  7. Android(安卓)获取动态权限时候用户选择不再提示后无法获取权限
  8. Android手势翻页实现
  9. [Android]APP中保持屏幕不自动灭屏的一种方法

随机推荐

  1. go语言中goroutine的使用详解
  2. go微服务框架go-micro整体架构介绍
  3. 完全掌握Go的pprof使用方法
  4. vscode中的Golang配置介绍
  5. go语言中并发图文教程
  6. “12306”的架构到底有多牛逼?
  7. go语言使用revel框架实现用户注册教程(附
  8. golang能否替代php
  9. go语言中channel的详细介绍
  10. .go是什么文件