桌面快捷方式

  在做app过程中,有可能在设置里面有设置添加桌面快加方式的功能。下面就添加shortcut 研究下

  首先判断一下桌面是否已经添加了快捷方式:

判断api版本 以2.2版本的api 为判断标准

通过ContentProvier查询快捷方式数据

String url="";

  if(android.os.Build.VERSION.SDK_INT<8){

    url="content://com.android.launcer.settings/favorites?notify=true";

}else{

  url="content://com.android.launcer.settings2/favorites?notify=true";

}

 ContentResolver  resolver=context.getContentResolver();  //内容解析器

Cursor cursor=resolver.query(Uri.parse(url),null,"title=?",new String[]{context.getString(R.string.app_name)},null);

if(cursor!=null&&cursor.moveToFirst()){

      cursor.close();

}

判定完毕符合条件的话执行addshortcut方法

String ACTION_ADD_SHORTCUT="com.android.launcer.action.INSTALL_SHORTCUT";  //Intent  添加快捷方式 的action值

//也可以调用Manifest 类里面的静态低层内部类permission里的常量 INSTALL_SHORTCUT   但是api大于等于9

Intent intent=new Intent(ACTION_ADD_SHORTCUT);

ShortcutIconResource icon=Intent.ShortcutIconResource.fromContext(context,R.drawable.icon);  //获取快件方式图片

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

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON.RESOURCE,icon);

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

//设置切入的主activity

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(getApplicationContext(),AppMainActivity.class));

//或者使用component 

Intent maincutintent=new Intent();

maincutintent.setComponent(new ComponentName(context.getPackageName(),context.getPackageName()+".AppMainActivity"));

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,maincutintent);

context.sendBoradcast(intent);  //发送广播


删除快件方式使用 

action 只为  com.android.launcher.action.UNINSTALL_SHORTCUT  的intent执行任务





  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值