Android-contentprovider use
one set up ContentProvider server side
1 Create a class that inherits from ContentProvider and overrides the interface method (just a few logs are represented here)
PackageCom.chengzhi.androidcontentprovider;ImportAndroid.content.ContentProvider;ImportAndroid.content.ContentValues;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.util.Log;ImportAndroid.widget.Toast;/** * ContentProvider Server-side class * @author Chengzhi * */ Public class mycontentprovider extends contentprovider{ 1,//Override Delete method @Override Public int Delete(Uri arg0, String arg1, string[] arg2) {//TODO auto-generated method stub if(Arg0.getauthority (). Contains ("Com.chengzhi.androidcontentprovider") {LOG.I ("Chengzhi Log","Delete"); }return 0; }2,//Override GetType Method @Override PublicStringGetType(Uri arg0) {//TODO auto-generated method stub if(Arg0.getauthority (). Contains ("Com.chengzhi.androidcontentprovider") {LOG.I ("Chengzhi Log","GetType"); }return NULL; }3,//Override Insert Method @Override PublicUriInsert(Uri arg0, contentvalues arg1) {//TODO auto-generated method stub if(Arg0.getauthority (). Contains ("Com.chengzhi.androidcontentprovider") {LOG.I ("Chengzhi Log","Insert"); }return NULL; }@Override Public Boolean onCreate() {//TODO auto-generated method stub return false; }4,//Rewrite the Query method @Override PublicCursorQuery(Uri arg0, string[] arg1, String arg2, string[] arg3, string arg4) {//TODO auto-generated method stub if(Arg0.getauthority (). Contains ("Com.chengzhi.myprovider") {LOG.I ("Chengzhi Log","Query"); }return NULL; }5,//Override Update method @Override Public int Update(Uri arg0, contentvalues arg1, String arg2, string[] arg3) {//TODO auto-generated method stub if(Arg0.getauthority (). Contains ("Com.chengzhi.androidcontentprovider") {LOG.I ("Chengzhi Log","Updata"); }return 0; }}
2 registering this class in the manifest file
<provider android:name="MyContentProvider" android:authorities="com.chengzhi.myprovider"(可以自定义)></provider>
two classes to establish the client
Use the Contentresolve object primarily to manipulate server-side interface methods
Set the event listener for a button
button _buttonquery = (Button) Findviewbyid (r.id.buttonquery); _buttonquery.setonclicklistener (new Onclicklistener () { @Override public void onclick (View arg0) {//TODO auto-g enerated method Stub Contentresolver _contentresolver = Getcontentresolver (); //must be prefixed with content://standard prefix Cursor _cursor = _contentresolver.query (Uri.parse ( "Content://com.chengzhi.myprovider" ), null , null , null , null ); } });
Three notes
1 in the server-side query method, using the Getcontentresolver (). Query (), try using the Startmanagercursor (cursor) method to move the cursor's
The life cycle is hosted on the current activity so that the cursor's life cycle and activity are automatically synchronized to prevent the cursor from being compromised after the current activity exits
2 The current activity invokes the contentprovider of another process that does not start,
Android:multiprocess= "true" in provider in the current project's manifest file allows other processes to access
3 mechanism of the ContentProvider
When the program is installed and uninstalled, the ContentProvider class register and the unregister are put into the providermanager of the system,
When using Contentresolver to pass in a URI
Find the URI in the Providermanager in the list to match and start a different process
So the process is not started by activity, but by provider.
Binder mechanism
4 Life cycle of the host process
The host process is started when the call is received and the host process is empty after the operation is completed
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-contentprovider use