直接从数据库里修改桌面图标

 

1、定义bean对象。
public class Shortcut {
 public final long id;
 public final String title;
 public final String intent;
 public final String iconResource;
 
 public Shortcut(long id, String title, String intent, String iconResource) {
  this.id = id;
  this.title = title;
  this.intent = intent;
  this.iconResource = iconResource;
 }
 
 public long getId() {
  return id;
 }
 public String getTitle() {
  return title;
 }
 public String getIntent() {
  return intent;
 }
 public String getIconResource() {
  return iconResource;
 }
 
 public String toString() {
  return "id:" + id + ",title:" + title + ",intent:" + intent + ",icon:" + iconResource;
 }
}
 
2、定义dao类。
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
public class ShortcutDao {
 private ContentResolver contentResolver;
 private Uri uri = Uri.parse("content://com.android.launcher.settings/favorites?notify=true");
 private String[] columns = new String[]{"_id", "title", "intent", "iconResource"};
 
 public ShortcutDao(ContentResolver contentResolver){
  this.contentResolver = contentResolver;
 }
 
  public Shortcut getShortcut(String appName, String intent){
     String whereQuery = "title=? and intent like ?";
     String[] whereParams = new String[]{appName, "%"+intent+"%"};
     Cursor c = contentResolver.query(uri, columns, whereQuery, whereParams, null);
    
     try {
      c.moveToFirst();
            if (c.isAfterLast()){
                return null;
            }else{
             return new Shortcut(c.getLong(0), c.getString(1), c.getString(2), c.getString(3));
            }
        } finally {
         c.close();
        }
    }
    public void updateShortcut(long id, String icon){
     ContentValues values = new ContentValues();
        values.put("iconResource", icon);
        contentResolver.update(uri, values, "_id=?", new String[]{String.valueOf(id)});
    }

    public void updateShortcut(long id, String icon, String iconPackage){
     ContentValues values = new ContentValues();
        values.put("iconResource", icon);
        values.put("iconPackage", iconPackage);
        values.put("itemType", 1);
        contentResolver.update(uri, values, "_id=?", new String[]{String.valueOf(id)});
    }
}
 
3、添加下面这些权限。

 
 
 
 

你可能感兴趣的:(android,数据库,string,android,class,dao,null)