ui更新数据库 记录

来源:互联网 发布:力港网络客服电话 编辑:程序博客网 时间:2024/05/15 08:32
import android.content.ContentProvider;import android.content.ContentValues;import android.content.UriMatcher;import android.database.Cursor;import android.net.Uri;public class MyContentProvider extends ContentProvider { private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); private static final int CALL=1; private static final int APP=2; private static final int SMS=3; private static final int MUSIC=4; private static final int SNS=5; private static final int WORD=6; private static final int BROWSER=7; private static final int TIME=8; static{          MATCHER.addURI("provider.MyContentProvider", "call", CALL);        MATCHER.addURI("provider.MyContentProvider","app", APP);        MATCHER.addURI("provider.MyContentProvider", "sms", SMS);        MATCHER.addURI("provider.MyContentProvider", "music", MUSIC);        MATCHER.addURI("provider.MyContentProvider", "sns", SNS);        MATCHER.addURI("provider.MyContentProvider", "word", WORD);        MATCHER.addURI("provider.MyContentProvider", "browser",BROWSER);        MATCHER.addURI("provider.MyContentProvider", "time",TIME);    }   private DBHandler dbHandler=null;@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {// TODO Auto-generated method stubreturn 0;}@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubreturn null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {// TODO Auto-generated method stubswitch (MATCHER.match(uri)) {case CALL: dbHandler.insert("call", values);getContext().getContentResolver().notifyChange(uri, null); getContext().getContentResolver().notifyChange(TIME_URI, null);return uri;case APP: dbHandler.insert("appstate", values);getContext().getContentResolver().notifyChange(uri, null); getContext().getContentResolver().notifyChange(TIME_URI, null);return uri;case SMS:dbHandler.insert("sms", values);getContext().getContentResolver().notifyChange(uri, null); getContext().getContentResolver().notifyChange(TIME_URI, null);return uri;case MUSIC:dbHandler.insert("music", values);getContext().getContentResolver().notifyChange(uri, null); getContext().getContentResolver().notifyChange(TIME_URI, null);return uri;case SNS:dbHandler.insert("sns", values);getContext().getContentResolver().notifyChange(uri, null); getContext().getContentResolver().notifyChange(TIME_URI, null);return uri;case WORD:dbHandler.insert("search", values);getContext().getContentResolver().notifyChange(uri, null); getContext().getContentResolver().notifyChange(TIME_URI, null);return uri;case BROWSER:dbHandler.insert("browser", values);getContext().getContentResolver().notifyChange(uri, null); getContext().getContentResolver().notifyChange(TIME_URI, null);return uri;default:break;}return null;}@Overridepublic boolean onCreate() {LauncherActivity.context = getContext();if(dbHandler==null)dbHandler=DBHandler.getInstance();return true;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {// TODO Auto-generated method stubCursor cursor=null;switch (MATCHER.match(uri)) {case CALL:cursor = dbHandler.rawQuery("select _id,people,number,duration,is_incoming, datetime(date,'unixepoch','localtime')  from call order by date desc");cursor.setNotificationUri(getContext().getContentResolver(), uri);return cursor;case APP: cursor = dbHandler.rawQuery("select _id,package_name,state,datetime(date,'unixepoch','localtime')  from appstate order by date desc");cursor.setNotificationUri(getContext().getContentResolver(), uri);return cursor;case SMS:cursor = dbHandler.rawQuery("select sms._id, sms.number,textcontent.content,sms.is_inbox, datetime(sms.date,'unixepoch','localtime') from sms inner join textcontent on sms.textcontent_id=textcontent._id order by sms.date desc");cursor.setNotificationUri(getContext().getContentResolver(), uri);return cursor;case MUSIC:cursor = dbHandler.rawQuery("select _id,title,artist,album,genre,composer,name,duration,datetime(date,'unixepoch','localtime') date,filepath from music order by date desc");cursor.setNotificationUri(getContext().getContentResolver(), uri);return cursor;case SNS:cursor = dbHandler.rawQuery("select sns._id, textcontent.content,sns.type,datetime(sns.date,'unixepoch','localtime'),sns.sid from sns inner join textcontent on sns.textcontent_id=textcontent._id order by sns.date desc");cursor.setNotificationUri(getContext().getContentResolver(), uri);return cursor;case WORD:cursor = dbHandler.rawQuery("select _id, words,datetime(date,'unixepoch','localtime')  from search order by date desc");cursor.setNotificationUri(getContext().getContentResolver(), uri);return cursor;case BROWSER:cursor = dbHandler.rawQuery("select _id,url,title,visits, datetime(date,'unixepoch','localtime')  from browser order by date desc");cursor.setNotificationUri(getContext().getContentResolver(), uri);return cursor;case TIME:cursor = dbHandler.rawQuery("select * from ( select _id, title t1,url t2, datetime(date,'unixepoch','localtime') date, 'browser' type,'bro' path from browser union all select _id, people t1, number t2,datetime(date,'unixepoch','localtime') date, 'call' type,'bro' path  from call union all select _id,title t1, artist t2,datetime(date,'unixepoch','localtime') date,'music' type,filepath path from music union all select sns._id,sns.type t1,textcontent.content t2, datetime(sns.date,'unixepoch','localtime') date,'SNS' type,'bro' path from sns left join  textcontent on textcontent._id=sns.textcontent_id union all select sms._id,number t1,textcontent.content t2, datetime(sms.date,'unixepoch','localtime') date,'SMS' type,'bro' path from sms left join  textcontent on textcontent._id=sms.textcontent_id union all select _id, '' t1, words t2, datetime(date,'unixepoch','localtime') date,'search' type,'bro' path from search union all select _id,package_name t1,state t2,datetime(date,'unixepoch','localtime') date,'app' type,'bro' path from appstate) order by date desc");cursor.setNotificationUri(getContext().getContentResolver(), uri);return cursor;default:break;}return null;}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {// TODO Auto-generated method stubreturn 0;}}


下个是 cursorloader

import android.content.Context;import android.content.CursorLoader;import android.database.Cursor;import android.net.Uri;public class MyCursorLoader extends CursorLoader { Cursor mCursor;public MyCursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {super(context, uri, projection, selection, selectionArgs, sortOrder);} @Override    public void onContentChanged() {        if (!mCursor.isClosed()) {            deliverResult(mCursor);        }        forceLoad();    }    @Override    public Cursor loadInBackground() {        mCursor = super.loadInBackground();        return mCursor;    }}



 

原创粉丝点击