设计目标
contentprovider是安卓四大组件之一,使用其方法类进行数据获取
通过contentprovider实现在不同软件之间的数据传输
通过uri来标识其它应用要访问的数据,通过ContentResolver的增、删、改、查方法实现对共享数据的操作。
还可以通过注册ContentObserver来监听数据是否发生了变化来对应的刷新页面。
URI(Universal Resource Identifier)统一资源定位符
代码
provider
MyDAO
1 package com.example.provider; 2 3 import android.content.ContentUris; 4 import android.content.ContentValues; 5 import android.content.Context; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.net.Uri; 8 9 public class MyDAO { 10 private Context context; 11 private SQLiteDatabase database; 12 13 public MyDAO(Context context) { 14 this.context = context; 15 MyDBhelper dBhelper = new MyDBhelper(context, "testDB", null, 1); 16 database = dBhelper.getWritableDatabase(); 17 } 18 19 public Uri DAOinsert(ContentValues contentValues) { 20 long rowid=database.insert("student", null, contentValues); 21 // Uri uri=Uri.parse("content://test.provider2/student/10");//取第10行 22 Uri uri=Uri.parse("content://test.provider2"); 23 Uri inserturi=ContentUris.withAppendedId(uri,rowid); 24 context.getContentResolver().notifyChange(inserturi,null); 25 return inserturi; 26 } 27 28 }
MyDBhelper
1 package com.example.provider; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 import android.util.Log; 7 8 import androidx.annotation.Nullable; 9 10 public class MyDBhelper extends SQLiteOpenHelper { 11 public MyDBhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { 12 super(context, name, factory, version); 13 } 14 15 @Override 16 public void onCreate(SQLiteDatabase sqLiteDatebase) { 17 // SQLiteDatabase sqLiteDatebase = null; 18 sqLiteDatebase.execSQL("create table student(" + 19 "id integer primary key autoincrement,name varchar(20),age integer)"); 20 } 21 @Override 22 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 23 24 } 25 }
MainActivity
1 package com.example.provider; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 7 public class MainActivity extends AppCompatActivity { 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 14 15 MyDAO myDAO=new MyDAO(this); 16 } 17 }
MyContentProvider
1 package com.example.provider; 2 3 import android.content.ContentProvider; 4 import android.content.ContentValues; 5 import android.content.Context; 6 import android.database.Cursor; 7 import android.net.Uri; 8 9 public class MyContentProvider extends ContentProvider { 10 private MyDAO myDAO; 11 12 public MyContentProvider() { 13 14 15 } 16 17 @Override 18 public int delete(Uri uri, String selection, String[] selectionArgs) { 19 // Implement this to handle requests to delete one or more rows. 20 throw new UnsupportedOperationException("Not yet implemented"); 21 } 22 23 @Override 24 public String getType(Uri uri) { 25 26 // TODO: Implement this to handle requests for the MIME type of the data 27 // at the given URI. 28 throw new UnsupportedOperationException("Not yet implemented"); 29 } 30 31 @Override 32 public Uri insert(Uri uri, ContentValues values) { 33 34 return myDAO.DAOinsert(values); 35 // TODO: Implement this to handle requests to insert a new row. 36 // throw new UnsupportedOperationException("Not yet implemented"); 37 } 38 39 @Override 40 public boolean onCreate() { 41 42 Context context=getContext(); 43 myDAO=new MyDAO(context); 44 // TODO: Implement this to initialize your content provider on startup. 45 return false; 46 } 47 48 @Override 49 public Cursor query(Uri uri, String[] projection, String selection, 50 String[] selectionArgs, String sortOrder) { 51 // TODO: Implement this to handle query requests from clients. 52 throw new UnsupportedOperationException("Not yet implemented"); 53 } 54 55 @Override 56 public int update(Uri uri, ContentValues values, String selection, 57 String[] selectionArgs) { 58 // TODO: Implement this to handle requests to update one or more rows. 59 throw new UnsupportedOperationException("Not yet implemented"); 60 } 61 }
resolver
MainActivity
1 package com.example.resolver; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.ContentResolver; 6 import android.content.ContentValues; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.Button; 11 12 public class MainActivity extends AppCompatActivity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 ContentResolver resolver=getContentResolver(); 19 Button button=findViewById(R.id.button); 20 21 ContentValues values=new ContentValues(); 22 values.put("name","myinsert"); 23 values.put("age",99); 24 25 Uri uri=Uri.parse("content://test.provider2/student"); 26 27 button.setOnClickListener(new View.OnClickListener() { 28 @Override 29 public void onClick(View v) { 30 resolver.insert(uri,values); 31 32 } 33 }); 34 } 35 }
运行结果
项目地址
https://github.com/yoyo-sanchez/yoyo/tree/main/contentProvider
标签:安卓,Uri,new,content,provider,import,android,public From: https://www.cnblogs.com/yoyosanchez/p/16923038.html