private final static String TAG = “TestNameActivity”;
private Button mQueryAll;
private TextView TV_query_all;
private Button mRemoveAll;
private Button mInitAll;
private EditText add_one1;
priv
ate EditText add_one2;
private Button mAddOne;
private Button mRemoveOne;
private String s="";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.namelist);
mQueryAll = (Button)findViewById(R.id.query_all);
mQueryAll.setOnClickListener(this);
TV_query_all = (TextView)findViewById(R.id.tv_query_all);
mRemoveAll = (Button)findViewById(R.id.remove_all);
mRemoveAll.setOnClickListener(this);
mInitAll = (Button) findViewById(R.id.init_all);
mInitAll.setOnClickListener(this);
add_one1 = (EditText) findViewById(R.id.et_1);
add_one2 = (EditText) findViewById(R.id.et_2);
mAddOne = (Button) findViewById(R.id.add_one);
mAddOne.setOnClickListener(this);
mRemoveOne = (Button) findViewById(R.id.remove_one);
mRemoveOne.setOnClickListener(this);
}
@Override
public void onClick(View v) {
ContentResolver resolver = getContentResolver();
if(v.getId() == R.id.query_all) {
Cursor cursor = resolver.query(NameListProvider.all_name_uri, null, null, null,null);
boolean hasFirst = cursor.moveToFirst();
try {
if(hasFirst) {
do {
String pkg = cursor.getString(cursor.getColumnIndex(“pkg”));
String type = cursor.getString(cursor.getColumnIndex(“type”));
// Log.i(TAG, " pkg=" + pkg + " type=" + type);
s+=“pkg=”+pkg+"\n" +“type=”+type+"\n";
} while (cursor.moveToNext());
TV_query_all.setText(s);
}else {
Log.i(TAG, “无数据,请先初始化数据”);
}
} finally {
cursor.close();
}
try {
Thread.currentThread().sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else if(v.getId() == R.id.remove_all) {
int rowId = resolver.delete(NameListProvider.all_name_uri, null, null);
}else if(v.getId() == R.id.init_all) {
Intent i = new Intent();
i.setAction(“cn.nubia.small_window_namelist_update”);
sendBroadcast(i);
}else if(v.getId() == R.id.add_one) {//add_one
ContentValues values = new ContentValues();
if (add_one1.getText().toString().equals("")&&add_one2.getText().toString().equals("")){
Toast.makeText(this, “请输入pkg和type”, Toast.LENGTH_SHORT).show();
}else {
values.put(“pkg”, add_one1.getText().toString());
values.put(“type”,add_one2.getText().toString());
resolver.insert(NameListProvider.all_name_uri, values);
}
}else if(v.getId() == R.id.remove_one) {//add_one
String where = “pkg = ?”;
String[] args = new String[] {“test_add_one”};
int rowId = resolver.delete(NameListProvider.all_name_uri, where, args);
}
}
}
2.NameListDBHelper.java中:
public class NameListDBHelper extends SQLiteOpenHelper {
private final static String TAG = “NameListDataBaseHelper”;
private static NameListDBHelper mInstance;
private final String CREATE_NAME_LIST_TABLE = “create table " + TABLE_NAME + " (” +
"id integer primary key autoincrement, " +
"pkg text, " +
“type text)”;
private final static int DB_VERSION = 1;
public final static String TABLE_NAME = “smallwindow”;
public NameListDBHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
public static synchronized NameListDBHelper getInstance(Context context) {
if(mInstance == null) {
mInstance = new NameListDBHelper(context, “name_list.db”, null, DB_VERSION);
}
return mInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, “onCreate DB , DB_VERSION=” + DB_VERSION);
db.execSQL(CREATE_NAME_LIST_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, “onUpgrade , oldVersion=” + oldVersion + " newVersion=" + newVersion);
}
public void add(List infos) {
if(infos != null) {
SQLiteDatabase db = mInstance.getWritableDatabase();
db.delete(TABLE_NAME, null, null);
for (AppInfo info : infos) {
ContentValues values = new ContentValues();
values.clear();
values.put(“pkg”, info.getPkg());
values.put(“type”, info.getType());
db.insert(TABLE_NAME, null, values);
}
}
}
}
3.NameListProvider.java
中:
public class NameListProvider extends ContentProvider {
private final static String TAG = “NameListProvider”;
private SQLiteDatabase db = null;
private static UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private final static String AUTHORITY = “cn.nubia.smallwindow.provider”;
private final static int APPINFO = 1;
private final static int APPINFOS = 2;
private final static String TABLE = NameListDBHelper.TABLE_NAME;
public final static Uri all_name_uri = Uri.parse(“content://” + AUTHORITY + “/appinfo”);
static {
MATCHER.addURI(AUTHORITY, “appinfo/#”, APPINFO);
MATCHER.addURI(AUTHORITY, “appinfo”, APPINFOS);
}
@Override
public boolean onCreate() {
db = NameListDBHelper.getInstance(getContext()).getWritableDatabase();
return db != null;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
int matchType = MATCHER.match(uri);
Cursor cursor = null;
switch (matchType) {
n onCreate() {
db = NameListDBHelper.getInstance(getContext()).getWritableDatabase();
return db != null;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
int matchType = MATCHER.match(uri);
Cursor cursor = null;
switch (matchType) {