1.作业内容

  • 1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;

  • 2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。

  • 3、本次作业请启用新项目,理论上需要两个APP进行实验。

2.作业目标

  • 1、ContentProvider可以帮助应用程序管理对自己存储的数据的访问, 由其他应用存储,并提供与其他应用共享数据的方法。它们封装了数据,并提供定义数据安全性的机制。内容提供商是标准 将一个进程中的数据与另一个进程中运行的代码连接起来的接口。

  • 2、在本次实验中实现resolver中调用调用provider的insert方法,在provider中创建的数据库中进行增操作。

3.核心代码

provider中实现数据库的创建和基本操作,并定义

1.provider app

MainActivity.java

1
2
3
4
5
6
7
8
9
10
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MyDAO myDAO=new MyDAO(this);
}
}

数据库部分

Mydbhelper.java:定义数据库,创建学生表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Mydbhelper extends SQLiteOpenHelper {
public Mydbhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table student(" +
"id integer primary key autoincrement,name varchar,age integer)");
Log.d("dolly","onCreate...");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
Log.d("dolly","onUpgrade...");
}
}

MyDAO.java:数据库操作类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MyDAO {
private Context context;
private SQLiteDatabase database;
public MyDAO(Context context){
this.context=context;

Mydbhelper dBhelper =new Mydbhelper(context,"dollyDB",null,1);
database=dBhelper.getReadableDatabase();
}

public Uri DAOinsert(ContentValues contentValues) {
long rowid = database.insert("student", null, contentValues);

Uri uri = Uri.parse("content://dolly.provider2/student");
Uri inserturi = ContentUris.withAppendedId(uri, rowid);
context.getContentResolver().notifyChange(inserturi, null);
return inserturi;

}
}

MyContentProvider.java: 提供insert方法供resovler使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class MyContentProvider extends ContentProvider {
private MyDAO myDAO;
public MyContentProvider() {
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
return myDAO.DAOinsert(values);
}

@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
Context context=getContext();
myDAO=new MyDAO(context);
return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
}

2.resovler app

** MainActivity.java**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ContentResolver resolver=getContentResolver();

ContentValues values=new ContentValues();
values.put("name","dolly");
values.put("age",20);

Uri uri=Uri.parse("content://dolly.provider2/student");

Button button = findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resolver.insert(uri,values);
}
});

}
}

4.结果演示

界面:

 点击insert向student表中添加数据:如下

5.源码仓库地址

Provider
Resovler