这篇文章给大家介绍ContentProvider怎么在Android应用中实现自定义,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
泗阳ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
1,创建一个数据库帮助类,归根结底都是它在操作数据库。代码如下:
package com.njue; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBManager extends SQLiteOpenHelper { public static final String myProvider_table_name="myProvider_table_name"; String sql="create table myProvider_table_name(id integer,name text)"; public DBManager(Context context) { super(context, "myPrivoder.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub arg0.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
2,创建一个myProvider类,该类继承ContentProvider,实现其抽象方法来操作数据库,该类引用DBManager类来获得数据库实例。
package com.njue; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; public class myProvider extends ContentProvider { DBManager dbHelper; @Override public int delete(Uri arg0, String arg1, String[] arg2) { // TODO Auto-generated method stub SQLiteDatabase db=dbHelper.getWritableDatabase(); int id=db.delete(DBManager.myProvider_table_name, arg1, arg2); if(id!=0)return id; 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 SQLiteDatabase db=dbHelper.getWritableDatabase(); db.insert(DBManager.myProvider_table_name, "id", values); return null; } @Override public boolean onCreate() { // TODO Auto-generated method stub dbHelper=new DBManager(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub SQLiteDatabase db=dbHelper.getWritableDatabase(); Cursor c=db.query(DBManager.myProvider_table_name,projection, selection, selectionArgs, null, null, sortOrder); return c; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub SQLiteDatabase db=dbHelper.getWritableDatabase(); int id=db.update(DBManager.myProvider_table_name, values, selection, selectionArgs); if(id!=0)return id; return 0; } }
3,ContextProvider必须声明才能使用,因此,需要在AndroidManifest.xml配置文件中声明。声明很简单,需要类名称和授权名称两个属性。
到这里,我们自定义的ContentProvider就完成了。我们可以在Eclipse中的File Explorer 窗口中看到自定义的ContentProvider
4,简单测试
package com.njue; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Content_providerActivity extends Activity { /** Called when the activity is first created. */ Uri uri; String AUTHORITY="content://com.njue.myProvider/myProvider_table_name"; ContentResolver cr; EditText input; Button insert; TextView show; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); input=(EditText)findViewById(R.id.input); insert=(Button)findViewById(R.id.insert); show=(TextView)findViewById(R.id.show); uri=uri.parse(AUTHORITY); cr=this.getContentResolver(); insert.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub ContentValues values=new ContentValues(); values.put("name", input.getText().toString()); cr.insert(uri, values); Cursor c=cr.query(uri, null, null, null, null); show.setText(""); c.moveToFirst(); while(c.moveToNext()){ show.append(c.getString(1)+"\r\n"); } input.setText(""); }}); } }
关于ContentProvider怎么在Android应用中实现自定义就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。