应用本地数据库
1、创建
首先在AndroidManifest.xml 中添加
<providerandroid:authorities="com.test.appinfo.provider" android:exported = "true"android:name="com.test.provider.AppInfoProvider" ></provider>
package com.test.provider;import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;import com.xiaoxun.util.XunConstant;/*** Created by zhanghaijun on 2018/11/27.*/public class AppInfoProvider extends ContentProvider {private static final String TAG = "AppInfoProvider";private static final String DATABASE_NAME = "XunAppInfo.db";private static final int DATABASE_VERSION = 1;private static final String TABLE_NAME = "xxappinfo";private SQLiteDatabase mDatabase;private DatabaseHelper mDatabaseHelper;/*** name : 支付宝* type : 1* app_id : com.xiaoxun.xun* EID : BAE73BE9E6B4BEF605CE787B8ACCD6B4* GID : F414F6631EB2F64D81B7916210C78660* optype : 0* icon : url//icon* status : 0* version : 1.2.8888* download_url : xxxx.xunkeys.com/uuu* wifi : 1* size : 1000* md5 : 45464454646464* updateTS : 20181220164734110*/public static final String AUTHORITY = "com.test.appinfo.provider";public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/xxappinfo");private static class DatabaseHelper extends SQLiteOpenHelper {private Context mContext;public DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);mContext = context;}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("Create table " + TABLE_NAME + "( _id INTEGER PRIMARY KEY AUTOINCREMENT,"+ "isSystem TEXT,"+ "name TEXT,"+ "type INTEGER,"+ "app_id TEXT,"+ "EID TEXT,"+ "GID TEXT,"+ "optype INTEGER,"+ "icon TEXT,"+ "status INTEGER,"+ "hidden INTEGER,"+ "version TEXT,"+ "version_code INTEGER,"+ "download_url TEXT,"+ "wifi INTEGER,"+ "size INTEGER,"+ "md5 TEXT,"+ "updateTS TEXT,"+ "version_code_new INTEGER,"+ "version_new TEXT,"+ "type_new INTEGER,"+ "icon_new TEXT,"+ "download_url_new TEXT,"+ "size_new INTEGER,"+ "md5_new TEXT,"+ "blank_one TEXT,"+ "blank_two TEXT,"+ "blank_three TEXT,"+ "blank_four TEXT,"+ "blank_five TEXT);");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);onCreate(db);}}@Overridepublic boolean onCreate() {mDatabaseHelper = new DatabaseHelper(getContext());return true;}@Overridepublic int delete(Uri url, String selection, String[] selectionArgs) {mDatabase = mDatabaseHelper.getWritableDatabase();getContext().getContentResolver().notifyChange(url, null, false);return mDatabase.delete(TABLE_NAME, selection, selectionArgs);}@Overridepublic String getType(Uri url) {return null;}@Overridepublic Uri insert(Uri url, ContentValues initialValues) {mDatabase = mDatabaseHelper.getWritableDatabase();ContentValues values = new ContentValues(initialValues);long rowId = mDatabase.insert(TABLE_NAME, null, values);if (rowId > 0) {Uri rowUri = ContentUris.appendId(CONTENT_URI.buildUpon(), rowId).build();getContext().getContentResolver().notifyChange(url, null, false);return rowUri;}throw new SQLException("jxring: Failed to insert row into " + url);}@Overridepublic Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs,String sortOrder) {SQLiteQueryBuilder qb = new SQLiteQueryBuilder();SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();qb.setTables(TABLE_NAME);Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);if (c != null) {c.setNotificationUri(getContext().getContentResolver(), url);}return c;}@Overridepublic int update(Uri url, ContentValues initialValues, String where, String[] whereArgs) {mDatabase = mDatabaseHelper.getWritableDatabase();ContentValues values = new ContentValues(initialValues);getContext().getContentResolver().notifyChange(url, null, false);return mDatabase.update(TABLE_NAME, values, where, whereArgs);}
}
2、使用
public static final String AUTHORITY = "com.test.appinfo.provider";public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/xxappinfo");//写入public void insertData_SystemAPk() {String[] mAppIds = LauncherApplication.getInstance().getResources().getStringArray(R.array.appstor_applists);String[] mappNames = LauncherApplication.getInstance().getResources().getStringArray(R.array.appstor_applists_name);String need_hiden = Settings.System.getString(LauncherApplication.getInstance().getContentResolver(), "XunAppHiden");if (need_hiden == null) need_hiden = "";try {ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();for (int i = 0; i < mAppIds.length; i++) {if (!mAppIds[i].equals("ado.install.xiaoxun.com.xiaoxuninstallapk")) {if (!mAppIds[i].equals("com.xxun.pointsystem")) {ops.add(ContentProviderOperation.newInsert(XunConstant.CONTENT_URI).withValue(XunConstant.NAME, mappNames[i]).withValue(XunConstant.TYPE, 2).withValue(XunConstant.APP_ID, mAppIds[i]).withValue(XunConstant.ICON, mAppIds[i]).withValue(XunConstant.STATUS, 0).withValue(XunConstant.HIDDEN, need_hiden.contains(mAppIds[i]) ? 1 : 0).withYieldAllowed(true).build());Log.d("zhj", "mappNames[i] = " + i + ", name =" + mappNames[i]);}}}LauncherApplication.getInstance().getContentResolver().applyBatch(XunConstant.AUTHORITY, ops);} catch (RemoteException reception) {} catch (OperationApplicationException oaeception) {}}//查询public static List<ListBean> queryNeedUpdateApk(Context mcontext){List<ListBean> mlist = null;Cursor mcursor = mcontext.getContentResolver().query(XunConstant.CONTENT_URI,new String[]{XunConstant.EID,XunConstant.WIFI,XunConstant.GID,XunConstant.TYPE,XunConstant.HIDDEN,XunConstant.ICON,XunConstant.VERSION_CODE,XunConstant.VERSION,XunConstant.OPTYPE,XunConstant.SIZE,XunConstant.DOWNLOAD_URL,XunConstant.NAME,XunConstant.APP_ID,XunConstant.STATUS,XunConstant.MD5,XunConstant.ATTR},XunConstant.STATUS+"=? ",new String[]{"2"},null);if(mcursor != null){mlist = new ArrayList<>();while (mcursor.moveToNext()){ListBean mPl = new ListBean();mPl.EID = mcursor.getString(mcursor.getColumnIndex(XunConstant.EID));mPl.wifi = mcursor.getInt(mcursor.getColumnIndex(XunConstant.WIFI));mPl.GID = mcursor.getString(mcursor.getColumnIndex(XunConstant.GID));mPl.type = mcursor.getInt(mcursor.getColumnIndex(XunConstant.TYPE));mPl.hidden = mcursor.getInt(mcursor.getColumnIndex(XunConstant.HIDDEN));mPl.icon = mcursor.getString(mcursor.getColumnIndex(XunConstant.ICON));mPl.version_code = mcursor.getInt(mcursor.getColumnIndex(XunConstant.VERSION_CODE));mPl.version = mcursor.getString(mcursor.getColumnIndex(XunConstant.VERSION));mPl.optype = 1;//mcursor.getInt(mcursor.getColumnIndex(XunConstant.OPTYPE));//0:add 1:modify 2:deletemPl.size = mcursor.getInt(mcursor.getColumnIndex(XunConstant.SIZE));mPl.downloadUrl = mcursor.getString(mcursor.getColumnIndex(XunConstant.DOWNLOAD_URL));mPl.name = mcursor.getString(mcursor.getColumnIndex(XunConstant.NAME));mPl.appId = mcursor.getString(mcursor.getColumnIndex(XunConstant.APP_ID));mPl.status = mcursor.getInt(mcursor.getColumnIndex(XunConstant.STATUS));mPl.md5 = mcursor.getString(mcursor.getColumnIndex(XunConstant.MD5));mPl.attr = mcursor.getString(mcursor.getColumnIndex(XunConstant.ATTR));mlist.add(mPl);}}return mlist;}public static PLBeanX getPLBeanX(String app_id,Context mcontext,int optype){PLBeanX mPl = null;Cursor mcursor = mcontext.getContentResolver().query(XunConstant.CONTENT_URI,new String[]{XunConstant.EID,XunConstant.WIFI,XunConstant.GID,XunConstant.TYPE,XunConstant.HIDDEN,XunConstant.ICON,XunConstant.VERSION_CODE,XunConstant.VERSION,XunConstant.OPTYPE,XunConstant.SIZE,XunConstant.DOWNLOAD_URL,XunConstant.NAME,XunConstant.APP_ID,XunConstant.STATUS,XunConstant.MD5,XunConstant.ATTR},XunConstant.APP_ID+" =?",new String[]{app_id+""},null);if(mcursor != null){while(mcursor.moveToNext()){mPl = new PLBeanX();mPl.EID = mcursor.getString(mcursor.getColumnIndex(XunConstant.EID));mPl.wifi = mcursor.getInt(mcursor.getColumnIndex(XunConstant.WIFI));mPl.GID = mcursor.getString(mcursor.getColumnIndex(XunConstant.GID));mPl.type = mcursor.getInt(mcursor.getColumnIndex(XunConstant.TYPE));mPl.hidden = mcursor.getInt(mcursor.getColumnIndex(XunConstant.HIDDEN));mPl.icon = mcursor.getString(mcursor.getColumnIndex(XunConstant.ICON));mPl.versionCode = mcursor.getInt(mcursor.getColumnIndex(XunConstant.VERSION_CODE));mPl.version = mcursor.getString(mcursor.getColumnIndex(XunConstant.VERSION));mPl.optype = optype;//mcursor.getString(mcursor.getColumnIndex(XunConstant.OPTYPE));//0:add 1:modify 2:deletemPl.size = mcursor.getInt(mcursor.getColumnIndex(XunConstant.SIZE));mPl.downloadUrl = mcursor.getString(mcursor.getColumnIndex(XunConstant.DOWNLOAD_URL));mPl.name = mcursor.getString(mcursor.getColumnIndex(XunConstant.NAME));mPl.appId = mcursor.getString(mcursor.getColumnIndex(XunConstant.APP_ID));mPl.status = mcursor.getInt(mcursor.getColumnIndex(XunConstant.STATUS));mPl.md5 = mcursor.getString(mcursor.getColumnIndex(XunConstant.MD5));mPl.attr = mcursor.getString(mcursor.getColumnIndex(XunConstant.ATTR));}mcursor.close();}return mPl;}//更新//更新一条数据库数据public static void AppstoreUpdate(ListBeanXX deal,Context context){if(!isHasData(deal.appId,context)){AppstoreInsert(deal,context);return; }try{ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();ops.add(ContentProviderOperation.newUpdate(XunConstant.CONTENT_URI).withSelection(XunConstant.APP_ID+" =?",new String[]{deal.appId+""}).withValue(XunConstant.NAME, deal.name).withValue(XunConstant.TYPE, deal.type).withValue(XunConstant.APP_ID, deal.appId).withValue(XunConstant.EID, deal.EID).withValue(XunConstant.GID, deal.GID).withValue(XunConstant.OPTYPE, deal.optype).withValue(XunConstant.ICON, deal.icon).withValue(XunConstant.STATUS, deal.status).withValue(XunConstant.HIDDEN, deal.hidden).withValue(XunConstant.VERSION, deal.version).withValue(XunConstant.VERSION_CODE, deal.version_code).withValue(XunConstant.DOWNLOAD_URL, deal.downloadUrl).withValue(XunConstant.WIFI, deal.wifi).withValue(XunConstant.SIZE, deal.size).withValue(XunConstant.MD5, deal.md5).withValue(XunConstant.UPDATES, deal.updateTS).withValue(XunConstant.ATTR, deal.attr).withYieldAllowed(true).build());context.getContentResolver().applyBatch(XunConstant.AUTHORITY, ops);}catch(RemoteException reception){}catch(OperationApplicationException oaeception){}}public static void AppstoreInsert(ListBeanXX deal,Context context){if(isHasData(deal.appId,context)){AppstoreUpdate(deal,context);return; }try{ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();ops.add(ContentProviderOperation.newInsert(XunConstant.CONTENT_URI).withValue(XunConstant.NAME, deal.name).withValue(XunConstant.TYPE, deal.type).withValue(XunConstant.APP_ID, deal.appId).withValue(XunConstant.EID, deal.EID).withValue(XunConstant.GID, deal.GID).withValue(XunConstant.OPTYPE, deal.optype).withValue(XunConstant.ICON, deal.icon).withValue(XunConstant.STATUS, deal.status).withValue(XunConstant.HIDDEN, deal.hidden).withValue(XunConstant.VERSION, deal.version).withValue(XunConstant.VERSION_CODE, deal.version_code).withValue(XunConstant.DOWNLOAD_URL, deal.downloadUrl).withValue(XunConstant.WIFI, deal.wifi).withValue(XunConstant.SIZE, deal.size).withValue(XunConstant.MD5, deal.md5).withValue(XunConstant.ATTR, deal.attr).withValue(XunConstant.UPDATES, deal.updateTS).withYieldAllowed(true).build());context.getContentResolver().applyBatch(XunConstant.AUTHORITY, ops);}catch(RemoteException reception){}catch(OperationApplicationException oaeception){}}
3、实体类
package com.test.xiaoxuninstallapk;import android.net.Uri;/*** Created by 张海军on 2018/12/27.*/public class XunConstant {public static final String ISSYSTEM = "isSystem";public static final String NAME = "name";public static final String TYPE = "type";public static final String APP_ID = "app_id";public static final String EID = "EID";public static final String GID = "GID";public static final String OPTYPE = "optype";public static final String ICON = "icon";public static final String STATUS = "status";public static final String HIDDEN = "hidden";public static final String VERSION = "version";public static final String VERSION_CODE = "version_code";public static final String DOWNLOAD_URL = "download_url";public static final String WIFI = "wifi";public static final String SIZE = "size";public static final String MD5 = "md5";public static final String UPDATES = "updateTS";public static final String VERSION_CODE_NEW = "version_code_new";public static final String VERSION_NEW = "version_new";public static final String TYPE_NEW = "type_new";public static final String ICON_NEW = "icon_new";public static final String DOWNLOAD_URL_NEW = "download_url_new";public static final String SIZE_NEW = "size_new";public static final String MD5_NEW = "md5_new";public static final String AUTHORITY = "com.xiaoxun.appinfo.provider";public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/xxappinfo");public static final String XUN_INSERTDATABASE_INIT_ACTION = "com.xiaoxun.init.getsystem.app";public static final String XUN_UPLOAD_SYS_APP_TO_SERVER = "com.xiaoxun.upload.system.toserver";public static final String XUN_UPLOAD_UPDATE_SOME_STATES = "com.xiaoxun.upload.some.states";public static final String XUN_APPSTORE_REMOVE_APP_BRO = "com.xiaoxun.uninstall.app";public static final String XUN_APPSTORE_UPDATE_LOCAL = "com.xiaoxun.update.local.database";
}package com.xiaoxun.bean;import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;import org.json.JSONException;
import org.json.JSONObject;import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;/*** Created by zhanghaijun on 2018/12/17.*/
public class ListBeanXX {/*** EID : BAE73BE9E6B4BEF605CE787B8ACCD6B4* wifi : 1* GID : F414F6631EB2F64D81B7916210C78660* icon : url//icon* type : 1* version : 1.2.8888* optype : 0* size : 1000* name : 支付宝* download_url : xxxx.xunkeys.com/uuu* app_id : com.xiaoxun.xun* status : 0* updateTS : 20181220164734110* md5 : 45464454646464*/@SerializedName("EID")public String EID;@SerializedName("wifi")public int wifi;@SerializedName("GID")public String GID;@SerializedName("icon")public String icon;@SerializedName("type")public int type;@SerializedName("version")public String version;@SerializedName("version_code")public int version_code;@SerializedName("hidden")public int hidden;@SerializedName("optype")public int optype;@SerializedName("size")public int size;@SerializedName("name")public String name;@SerializedName("download_url")public String downloadUrl;@SerializedName("app_id")public String appId;@SerializedName("status")public int status;@SerializedName("updateTS")public String updateTS;@SerializedName("md5")public String md5;@SerializedName("attr")public String attr;public static ListBeanXX objectFromData(String str) {return new Gson().fromJson(str, ListBeanXX.class);}public static ListBeanXX objectFromData(String str, String key) {try {JSONObject jsonObject = new JSONObject(str);return new Gson().fromJson(jsonObject.getString(str), ListBeanXX.class);} catch (JSONException e) {e.printStackTrace();}return null;}public static List<ListBeanXX> arrayListBeanXXFromData(String str) {Type listType = new TypeToken<ArrayList<ListBeanXX>>() {}.getType();return new Gson().fromJson(str, listType);}public static List<ListBeanXX> arrayListBeanXXFromData(String str, String key) {try {JSONObject jsonObject = new JSONObject(str);Type listType = new TypeToken<ArrayList<ListBeanXX>>() {}.getType();return new Gson().fromJson(jsonObject.getString(str), listType);} catch (JSONException e) {e.printStackTrace();}return new ArrayList();}
}
转载请标明出处: https://blog.csdn.net/hj_key/article/details/103453136