前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android极速开发之桌面快捷方式

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

作者头像
Javen
发布2018-08-21 10:59:19
1.2K0
发布2018-08-21 10:59:19
举报
文章被收录于专栏:酷玩时刻酷玩时刻酷玩时刻
  • 创建快捷方式1(logo图片使用资源文件R.drawable.xxx)
/**
     * 创建快捷方式
     * @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)
 /**
     * 创建快捷方式 
     * @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);

    }

两者的区别

        //第一种设置快捷方式图片
        Parcelable icon = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(), iconRes);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        //第二种设置快捷方式图片
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
  • 删除快捷键
 /**
     * 删除快捷键
     * 
     */
    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);
    }
  • 注意需要添加创建以及删除的权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
  • 一些栗子

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

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);
    }
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、创建快捷键点击启动安装页面

    /**
     * 快捷键启动安装界面
     * @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);
    }
public void startInstall(View view){
        ShortcutUtil.localAppIns(mContext, "安装应用", R.drawable.demo, Environment.getExternalStorageDirectory().getAbsoluteFile()+File.separator+"Download"+"nineapps.apk");
    }
  • 未成功的案例(动态创建快捷键,logo图片从网络加载)
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转化过来的才会创建成功

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

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

  • 快捷方式启动应用

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

private void setLauncherLogo(){
//隐式
Intent intent = new Intent("com.javen.test");
addShortcut(this,"隐式启动",R.mipmap.ic_launcher,intent,false);
}
<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>

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.08.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 如果有以上问题的解决方案以及疑问,欢迎留言讨论。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档