经过我们一番辛勤劳动过后,看到自己的程序在设备上面跑的时候,或多或少都有点成功感!但程序安装之后,一般来说,程序图标只显示在程序菜单之中,那在我的桌面上呢?有两种方式可以把程序的图标显示在桌面上:
第一:就是在程序菜单中,长按需要显示在桌面上的程序图标,当背景切换到桌面的时候,松开手指,程序的图标自然会显示在桌面上;
但是作为手机或者平板的使用者,追求的是方便,好用。我们当然需要满足他们的要求啦!接下来,说说在程序源码中添加桌面快捷方式,也是这篇文章的重点:
第二:源码中生成桌面快捷方式
第1步,在AndroidManifest.xml文件中添加相应的权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
第2步,在适合的地方添加生成快捷方式代码:
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra("duplicate",false);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(),R.drawable.ic_launcher);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
Intent intent = new Intent(getApplicationContext(),
ShortCutActivity.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
sendBroadcast(shortcutIntent);
编译,运行!看到快捷方式了吧?
本文介绍如何在Android设备上为应用程序创建桌面快捷方式。通过两种方法实现:一是手动从程序菜单添加,二是通过修改源代码自动生成。后者需在AndroidManifest.xml中添加特定权限,并在代码中使用Intent来创建快捷方式。
214

被折叠的 条评论
为什么被折叠?



