ui更新数据库 记录



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;


	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		
		switch (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;
	}

	@Override
	public boolean onCreate() {
		LauncherActivity.context = getContext();
		if(dbHandler==null)
			dbHandler=DBHandler.getInstance();
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		Cursor 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;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub

		return 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;
	    }

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值