创建或者删除桌面快捷方式

来源:互联网 发布:数据库第二版课后答案 编辑:程序博客网 时间:2024/04/30 10:51

添加快捷方式

private void addShortCut(){
  Intent intent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  intent.putExtra(intent.EXTRA_SHORTCUT_NAME,"paths");
  intent.putExtra("duplicate",false);
  
  Intent shortcutintent=new Intent(Intent.ACTION_MAIN);
  shortcutintent.setClassName(this, this.getClass().getName());
  
  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutintent );
  
  ShortcutIconResource shortcutIconResource=new ShortcutIconResource().fromContext(this,R.drawable.composer_place);
  intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIconResource);
  sendBroadcast(intent);
 }

删除快捷方式

private void deleteShortCut(){
     Intent deleteShortcut=new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
     deleteShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "paths");
     String appclass=this.getPackageName()+"."+this.getLocalClassName();
     ComponentName componentName=new ComponentName(this.getPackageName(),appclass);
     deleteShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(componentName));
     sendBroadcast(deleteShortcut);
    }

原创粉丝点击