• android程序桌面快捷方式的检测添加和删除


    废话不多说了,可能好多同志都在找这个功能的实现代码。下面就由我来讲讲吧!

    我的添加和删除快捷方式是在登陆的时候由用户选择的,效果是一个CheckBox,初始化界面的时候会查看用户以前是否已经勾选,再给他添加侦听,就能分别添加和删除快捷方式了。

    代码如下:

            saveShortcuts=(CheckBox) findViewById(R.id.saveshortcut);
            saveShortcuts.setOnCheckedChangeListener(clistener);
    

     监听并获取动作

    private OnCheckedChangeListener clistener= new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          if(buttonView.getId()==R.id.saveshortcut){
       sp=getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);//获取系统内保存的用户文件
        sp.edit().putBoolean("saveshortcuts", isChecked).commit();//将用户的选择保存到系统
        if(isChecked&&!hasShortcut()){//如果被选择,就执行检测和创建快捷方式
    addShortcut();
    }else if(!isChecked&&hasShortcut()){//否则就监测和删掉快捷方式
    delShortcut();
    }
    }
    }
    };

    处理动作:判断

     //检测是否存在快捷方式
    private boolean hasShortcut()
    {
    boolean isInstallShortcut = false;
    final ContentResolver cr = getContentResolver();
    //2.2版本的是launcher2,不然无效,网上有的是launcher,我试验了2.2不能用
    final Uri CONTENT_URI = Uri.parse("content://com.android.launcher2.settings/favorites?notify=true");//保持默认
    Cursor c = cr.query(CONTENT_URI,new String[] {"title","iconResource" },"title=?", //保持默认
    //getString(R.string.app_name)是获取string配置文件中的程序名字,这里用一个String的字符串也可以
    new String[] {getString(R.string.app_name).trim()}, null);
    if(c!=null && c.getCount()>0){
    isInstallShortcut = true ;
    }
    return isInstallShortcut ;
    }

    下面就是添加快捷方式了:

        //创建快捷方式
    private void addShortcut(){
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//保持默认
    //快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); //保持默认
    shortcut.putExtra("duplicate", false); //不允许重复创建
    Intent intent = new Intent(this,logo.class);//后面的logo.class是我的程序第一次加载的activity的名字,大家要注意
    intent.setAction("com.hooypay.Activity.logo");//这个也是logo的具体路径
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    //显示的图标
    Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.ic_launcher);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    sendBroadcast(shortcut);//广播
    }

    最后是删除快捷方式:

    //删除快捷方式
    private void delShortcut(){
    Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
    //快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

    //这里的intent要和创建时的intent设置一致
    Intent intent = new Intent(this,logo.class);
    intent.setAction("com.hooypay.Activity.logo");
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    sendBroadcast(shortcut);
    }


    以上是在参考网上的资料编写的。本人技术拙陋,如有错误,希望大家指正,谢谢

  • 相关阅读:
    Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动
    dubbox注解的一个坑
    内嵌Tomcat的Connector对象的静态代码块
    Spring Boot启动过程(三)
    Spring Boot启动过程(二)
    Spring Boot启动过程(一)
    SpringMVC基础学习(二)—开发Handler
    JS弹出框
    Oracle的基本学习(三)—函数
    Oracle的基本学习(二)—基本查询
  • 原文地址:https://www.cnblogs.com/winxiang/p/winxiang.html
Copyright © 2020-2023  润新知