編輯:關於Android編程
定義內容提供者可以開發自己已用的數據操作給第三方調用,下面的示例中用到了Android test case ,主要是為了給數據庫中添加數據方便內容提供者讀取。
1、聲明單元測試
2、創建數據庫工具類SQLiteOpenHelper
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyDBOpenHelper extends SQLiteOpenHelper { public MyDBOpenHelper(Context context) { super(context, "person.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table person (id integer primary key autoincrement,name varchar(20),number varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }3、創建AndroidTestCase,添加數據到數據庫
import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.test.AndroidTestCase; import com.am.cpv.db.MyDBOpenHelper; public class AddDataTestCase extends AndroidTestCase { public void testAdd() { MyDBOpenHelper oh = new MyDBOpenHelper(getContext()); SQLiteDatabase wdb = oh.getWritableDatabase(); // wdb.execSQL("insert into person(name) values(?)", new Object[]{"張三"}); for (int i = 0; i < 30; i++) { ContentValues values = new ContentValues(); values.put("name", "name" + i); values.put("number", "110-" + i); wdb.insert("person", null, values); } wdb.close(); } public void testQuery() { MyDBOpenHelper oh = new MyDBOpenHelper(getContext()); SQLiteDatabase wdb = oh.getWritableDatabase(); // Cursor cursor = wdb.rawQuery("select * from person ", new String[] {}); Cursor cursor2 = wdb.query("person", null, null, new String[] {}, null, null, null); while (cursor2.moveToNext()) { String[] columnNames = cursor2.getColumnNames(); for (String colName : columnNames) { int columnIndex = cursor2.getColumnIndex(colName); String val = cursor2.getString(columnIndex); System.out.print(val + " "); } System.out.println(); } } }
1、創建ContentProvider
import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import com.am.cpv.db.MyDBOpenHelper; public class PersonProvider extends ContentProvider { private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); private MyDBOpenHelper helper; private static final int QUERY = 1; static { uriMatcher.addURI("com.am.persionProvider", "query", QUERY); } @Override public boolean onCreate() { helper = new MyDBOpenHelper(getContext()); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // 判斷uri的請求是否為查詢 if (uriMatcher.match(uri) == QUERY) { SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor; } else { return 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 return null; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub return 0; } }
public void click(View v) { ContentResolver cr = getContentResolver(); Uri uri = Uri.parse("content://com.am.persionProvider/query"); Cursor cursor = cr.query(uri, null, "name=?", new String[] { "name1" }, null); while (cursor.moveToNext()) { String[] columnNames = cursor.getColumnNames(); for (String colName : columnNames) { int columnIndex = cursor.getColumnIndex(colName); String val = cursor.getString(columnIndex); System.out.print(val + " "); } } }
1、前言 今天項目要用到一個類似微信發送麼麼哒,那種屏幕飄表情的功能,所以分析研究了一下,用到的技術應該是路徑動畫,不知道這樣就正不正確,反正就是畫一個路徑線,然後對象根
ant 工具:1、為什麼要用到ant這個工具呢?Ant做為一種工具已經廣泛被使用,並且歷史悠久。使用ant的內置命令,可以編譯java源文件(javac),運行java文
首先新建了一個項目用來演示集成ShareSDK 下載好了ShareSDK之後,解壓sharesd
很久沒有更新第三方SDK這個系列了,所以更新一下這幾天工作中使用到的推送,寫這個系列真的很要命,你要去把他們的API文檔大致的翻閱一遍,而且各種功能都實現一遍,解決各種b