71创建应用程序的快捷方式
2014-12-22 21:38
246 查看
参考源码,知道创建快捷方式是通过发广播给receiver实现的。
第一步:
指定Action为:
第二步:
指定名称:
第三步:
指定图标:
第四步:
指定要干什么事情:
第五步:
发送广播:
运行,发现程序并没有创建快捷方式,查看logcat日志,发现了一点:需要添加权限:
现在运行,OK了,不过我们的程序每次进入都会创建一个快捷方式,这个太。。。所以我们需要知道当我们创建过了之后就不要再创建了:
完整的代码如下:
在onCreate()方法中调用。
第一步:
指定Action为:
com.android.launcher.action.INSTALL_SHORTCUT
第二步:
指定名称:
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手机管家");
第三步:
指定图标:
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
第四步:
指定要干什么事情:
Intent shortcutIntent = new Intent(); shortcutIntent.setAction(Intent.ACTION_MAIN); shortcutIntent.addCategory(Intent.CATEGORY_DEFAULT); shortcutIntent.setClassName(getPackageName(), "com.ustc.mobilemanager.SplashActivity"); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
第五步:
发送广播:
sendBroadcast(intent);
运行,发现程序并没有创建快捷方式,查看logcat日志,发现了一点:需要添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
现在运行,OK了,不过我们的程序每次进入都会创建一个快捷方式,这个太。。。所以我们需要知道当我们创建过了之后就不要再创建了:
完整的代码如下:
/**
* 创建快捷方式
*
*/
private void installShortcut() {
boolean shortcut = sp.getBoolean("shortcut", false);
if (shortcut) {
return;
}
Editor editor = sp.edit();
// 发送广播的Intent
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式,要包含三个重要的信息:1.名称;2.图标;3.干什么事情
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手机管家");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
Intent shortcutIntent = new Intent(); shortcutIntent.setAction(Intent.ACTION_MAIN); shortcutIntent.addCategory(Intent.CATEGORY_DEFAULT); shortcutIntent.setClassName(getPackageName(), "com.ustc.mobilemanager.SplashActivity"); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
sendBroadcast(intent);
Toast.makeText(this, "已创建手机管家快捷方式", 0).show();
editor.putBoolean("shortcut", true);
editor.commit();
}
在onCreate()方法中调用。
相关文章推荐
- 在ClickOnce应用程序中创建桌面快捷方式
- Android之快捷方式一——通过应用程序创建快捷方式
- 在应用程序中创建快捷方式
- 接收其他应用程序发出的创建快捷方式的广播
- Ubuntu系统应用程序创建快捷方式的方法
- 在ClickOnce应用程序中创建桌面快捷方式
- linux应用程序创建快捷启动方式
- Android开发之应用程序快捷方式的创建、删除、判断
- Android之快捷方式一——通过应用程序创建快捷方式
- Android 为应用程序中的任意Activity创建桌面快捷方式
- 给应用程序创建快捷方式VC实现
- linux\ubuntunutlinux下ubuntu 为应用程序创建快捷方式
- VC学习资料收集(24):在应用程序中创建快捷方式
- Android应用程序创建桌面快捷方式
- 在应用程序中创建快捷方式
- 应用程序创建快捷方式并判断是否创建(android)
- ubuntu 在桌面上创建应用程序图标(快捷方式)
- Ubuntu应用程序创建桌面快捷方式
- Ubuntu应用程序创建桌面快捷方式
- Linux Mint创建应用程序菜单及桌面快捷方式