contentprovider数据获取
生活随笔
收集整理的這篇文章主要介紹了
contentprovider数据获取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、實驗目的:
自建一個provider,然后在另一個app中使用resolver調用這個provider
二、實驗過程:
1.MyResolver對MyContentProvider表中添加一條數據操作
MainActivity.java
package com.example.myresolver;import androidx.appcompat.app.AppCompatActivity;import android.content.ContentResolver; import android.content.ContentValues; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button;public class MainActivity extends AppCompatActivity {private Button button;private ContentResolver resolver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=findViewById(R.id.button);Uri uri=Uri.parse("content://myz.provider/person");//建立聯系ContentResolver resolver=getContentResolver();ContentValues values=new ContentValues();values.put("name","myz");values.put("age",20);Integer.parseInt("20");button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {resolver.insert(uri,values);}});} }2.MyContentProvider建數據庫
MyDAO.java
package com.example.mycontentprovider;import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri; import android.util.Log; public class MyDAO {private SQLiteDatabase database;private SQLiteOpenHelper myopenhelper;private Context context;private Uri uri=Uri.parse("content://myz.provider");public MyDAO(Context context){this.context=context;myopenhelper=new MyDBhelper(context,"myzDB",null,1);database=myopenhelper.getReadableDatabase();database.execSQL("drop table if exists person");database.execSQL("create table person(id integer primary key autoincrement,"+" name varchar, age integer)");}public Uri addable(Uri uri, ContentValues values){long rowID=database.insert("person",null,values);if(rowID == -1){Log.d("DAO","數據插入失敗");return null;}else {Uri insertUri= ContentUris.withAppendedId(uri,rowID);Log.d("myz","ContentUris:"+insertUri.toString());context.getContentResolver().notifyChange(insertUri,null);return insertUri;}}}2.MyContentProvider建表
MyDBhelper.java
package com.example.mycontentprovider;import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;import androidx.annotation.Nullable;public class MyDBhelper extends SQLiteOpenHelper {public MyDBhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase) {sqLiteDatabase.execSQL("create table person(id integer primary key autoincrement " +",name varchar(20) ," +"age integer)");}@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {} }3.實現增添數據操作
MyContentProvider.java
package com.example.mycontentprovider;import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri; import android.util.Log; public class MyDAO {private SQLiteDatabase database;private SQLiteOpenHelper myopenhelper;private Context context;private Uri uri=Uri.parse("content://myz.provider");public MyDAO(Context context){this.context=context;myopenhelper=new MyDBhelper(context,"myzDB",null,1);database=myopenhelper.getReadableDatabase();database.execSQL("drop table if exists person");database.execSQL("create table person(id integer primary key autoincrement,"+" name varchar, age integer)");}public Uri addable(Uri uri, ContentValues values){long rowID=database.insert("person",null,values);if(rowID == -1){Log.d("DAO","數據插入失敗");return null;}else {Uri insertUri= ContentUris.withAppendedId(uri,rowID);Log.d("myz","ContentUris:"+insertUri.toString());context.getContentResolver().notifyChange(insertUri,null);return insertUri;}}}4.MainActivity.java
package com.example.mycontentprovider;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);MyDAO myDAO=new MyDAO(this);} }三、實驗結果
1.打開兩個app
2.運行MyResolver并點擊按鈕
?
3.查看MyContentProvider sqlite
?四、源碼
https://github.com/zappstg/homework3
總結
以上是生活随笔為你收集整理的contentprovider数据获取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单一道数学题 剿灭100%垃圾邮件
- 下一篇: Istio架构概览