Android极速开发之桌面快捷方式

欢迎留言、转发、打赏

项目参考地址:参考代码

  • 创建快捷方式1(logo图片使用资源文件R.drawable.xxx)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 创建快捷方式
* @param context
* @param shortcutName
* @param iconRes
* @param actionIntent
* @param allowRepeat
*/
public static void addShortcut(Context context, String shortcutName, int iconRes,Intent actionIntent,boolean allowRepeat){
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//是否允许重复创建
shortcutintent.putExtra("duplicate",allowRepeat);
//快捷方式的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
//设置快捷方式图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(), iconRes);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//设置快捷方式动作
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
//向系统发送广播
context.sendBroadcast(shortcutintent);
}
  • 创建快捷方式2(logo图标使用Bitmap)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 创建快捷方式
* @param context
* @param shotcutName
* @param bitmap
* @param actionIntent
* @param allowRepeat
*/
public static void addShortcut(Context context, String shotcutName, Bitmap bitmap,Intent actionIntent,boolean allowRepeat){
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//是否允许重复创建
shortcutintent.putExtra("duplicate",allowRepeat);
//快捷方式的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shotcutName);
//设置快捷方式图片
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
//设置快捷方式动作
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
//向系统发送广播
context.sendBroadcast(shortcutintent);
}

两者的区别

1
2
3
//第一种设置快捷方式图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(), iconRes);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
1
2
//第二种设置快捷方式图片
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
  • 删除快捷键
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 删除快捷键
*
*/
public void deleteShortcut(Context context, String name, int iconRes,Intent actionIntent,boolean allowRepeat){
Intent shortcutintent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
//是否循环删除
shortcutintent.putExtra("duplicate",allowRepeat);
//快捷方式的名称
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
//设置快捷方式动作
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
//向系统发送广播
context.sendBroadcast(shortcutintent);
}
  • 注意需要添加创建以及删除的权限

    1
    2
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
  • 一些栗子

1、创建快捷键点击打开浏览器

1
2
3
4
public static void openView(Context context,String appName,int iconRes,String url){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
addShortcut(context,appName,iconRes,intent,false);
}

1
2
3
4
5
public void shortcut(View view){
ShortcutUtil.openView(mContext, "启动浏览器", R.drawable.demo, "http://www.qq.com");
ShortcutUtil.openView(mContext, "启动浏览器2", ((BitmapDrawable)getResources().getDrawable(R.drawable.demo)).getBitmap(), "http://www.qq.com");
}

2、创建快捷键点击启动安装页面

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 快捷键启动安装界面
* @param context
* @param appName
* @param iconBitmap
* @param appPath
*/
public static void localAppIns(Context context,String appName,Bitmap iconBitmap,String appPath){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(new File(appPath)),"application/vnd.android.package-archive");
addShortcut(context,appName,iconBitmap,intent,false);
}
1
2
3
public void startInstall(View view){
ShortcutUtil.localAppIns(mContext, "安装应用", R.drawable.demo, Environment.getExternalStorageDirectory().getAbsoluteFile()+File.separator+"Download"+"nineapps.apk");
}
  • 未成功的案例(动态创建快捷键,logo图片从网络加载)
1
2
3
4
5
6
7
8
9
10
11
12
public void startInstall(View view){
VolleyUtils.loadImage(mContext, "http://www.pp3.cn/uploads/201608/20160801009.jpg", new MyImageListener() {
@Override
public void setImageBitmap(Bitmap bitmap) {
T.showLong(mContext, "下载完成");
image.setImageBitmap(bitmap);
ShortcutUtil.localAppIns(mContext, "安装应用", bitmap, Environment.getExternalStorageDirectory().getAbsoluteFile()+File.separator+"Download"+"nineapps.apk");
}
});
}

测试发现快捷方式并没有创建,Bitmap必须是资源文件中BitmapDrawable转化过来的才会创建成功

1
ShortcutUtil.localAppIns(mContext, "安装应用", ((BitmapDrawable)getResources().getDrawable(R.drawable.demo)).getBitmap(), Environment.getExternalStorageDirectory().getAbsoluteFile()+File.separator+"Download"+"nineapps.apk");

如果有以上问题的解决方案以及疑问,欢迎留言讨论。

  • 快捷方式启动应用

注意的是必须使用隐式启动的方式

1
2
3
4
5
private void setLauncherLogo(){
//隐式
Intent intent = new Intent("com.javen.test");
addShortcut(this,"隐式启动",R.mipmap.ic_launcher,intent,false);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="com.javen.test"/><!-- 自定义的action-->
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" /><!--必须加上这个,否则下面无法直接使用自定的action-->
</intent-filter>
</activity>

欢迎拍砖 项目参考地址:参考代码

Javen wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!