1.功能
用来实现跨程序数据共享的功能,如电话,短信,媒体库什么的都要给其它的程序共享数据,内容提供器可以选择一部分数据进行共享,保证我们的隐私数据
2.运行时权限
android6.0加入的,下面列举需要申请的危险权限
1.CALENDER(日历) : READ_CALENDAR WRITE_CALENDAR
2.CAMERA (相机) : CAMERA
3.CONTANTS (内容): READ_CONTANTS , WRITE_CONTANTS , GET_ACCOUNTS
4.LOCATION(位置): ACCESS_FINE_LOCATION , ACCESS_COARSE_LOCATION
5.MICROPHONE(麦克风): RECORD_AUDIO
6.PHONE(电话):READ_PHONE_STATE , CALL_PHONE , READ_CALL_LOG , WRITE_CALL_LOG , ADD_VOICEMAIL , USE_SIP , PROCESS_OUTGOING_CALLS
7.SENSORS(传感器) : BODY_SENSORS
8.SMS(短信) : SEND_SMS , RECEIVE_SMS , READ_SMS , RECEIVE_WAP_PUSH , RECEIVE_MMS
9.STORAGE(存储) : READ_EXTERNAL_STORAGE , WRITE_EXTERNAL_STORAGE
以下是我自己改进的申请权限的模板
<uses-permission android:name="android.permission.CALL_PHONE"/>
在开头新建一个List用来存储没有申请到的权限
List<String> notpermissed = new ArrayList<>();
要申请什么权限就在String[] 里面写好
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] permissions = new String[]{Manifest.permission.CALL_PHONE};
initPermission(permissions);
call();
}
});
private void initPermission(String[] permissions) {
notpermissed.clear();
for (int i=0;i<permissions.length;i++){
if (ContextCompat.checkSelfPermission(this,permissions[i]) != PackageManager.PERMISSION_GRANTED){
notpermissed.add(permissions[i]);
}
}
if (notpermissed.size()>0){
ActivityCompat.requestPermissions(this,permissions,1);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean permissed = false;
if (requestCode == 1){
for (int i=0;i<permissions.length;i++){
if (grantResults[i] == -1){
permissed = true;
}
}
if (permissed){
Toast.makeText(this, "权限未通过", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "权限通过", Toast.LENGTH_SHORT).show();
}
}
}
3.访问其他程序中的数据
增删改查都是使用一个Uri参数代替,被称为内容URI,通常写法为content://com.example.app.provider/table1
然后 Uri uri = Uri.parse(“content://com.example.app.provider/table1”);
Cursor cursor = getContentResolver().query(
uri,
projection,
selection,
selectionArgs,
sortOrder);
将数据从Cursor对象中提取出来
if(cursor !=null){
while(cursor.moveToNext()){
String column1 = cursor.getString(cursor.getColumnIndex("column1"));
int column2 = cursor.getInt(cursor.getColumnIndex("column2"));
}
cursor.close();
}
向table1表中添加数据
ContentValues values = new ContentValues();
values.put("column1","text");
values.put("column2",1);
getContentResolver().insert(uri,values);
更新
ContentValues values = new ContentValues();
values.put("column1","text");
getContentResolver().update(uri,values,"column1=? and column2 = ?",new String[]{"text","1"});
删除
getContentResolver().delete(uri,"column2=?",new String[]{"1"});
举例:读取电话簿里的联系人:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
List<String> list = new ArrayList<>();
ArrayAdapter<String> adapter;
ListView listView = findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] permissions = new String[]{Manifest.permission.READ_CONTACTS};
initPermission(permissions);
readcontent();
}
});
里面传入的Uri是做好封装之后的
private void readcontent() {
Cursor cursor = null;
try {
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null
, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String displayname = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
list.add(displayname + "\n" + number);
}
}
adapter.notifyDataSetChanged();
}catch (Exception e){
e.printStackTrace();
}finally {
if (cursor!=null)
cursor.close();
}
}
创建内容提供器
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.widget.Switch;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class MyProvider extends ContentProvider {
public static final int TABLE1_DIR = 0;
public static final int TABLE1_ITEM = 1;
public static final int TABLE2_DIR = 2;
public static final int TABLE2_ITEM = 3;
private static UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.example.app.provider","table1",TABLE1_DIR);
uriMatcher.addURI("com.example.app.provider","table1/#",TABLE1_ITEM);
uriMatcher.addURI("com.example.app.provider","table2",TABLE2_DIR);
uriMatcher.addURI("com.example.app.provider","table2/#",TABLE2_ITEM);
}
//初始化内容提供器的时候调用
@Override
public boolean onCreate() {
return false;
}
//从内容提供器中查询数据
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
switch (uriMatcher.match(uri)){
case TABLE1_DIR:
//查询table1的数据
break;
case TABLE1_ITEM:
//查询table1单条数据
break;
case TABLE2_DIR:
break;
case TABLE2_ITEM:
break;
}
return null;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
switch (uriMatcher.match(uri)){
case TABLE1_DIR:
return "vnd.android.cursor.dir/vnd.com.example.app.provider.table1";
case TABLE1_ITEM:
return "vnd.android.cursor.item/vnd.com.example.app.provider.table1";
case TABLE2_DIR:
return "vnd.android.cursor.dir/vnd.com.example.app.provider.table2";
case TABLE2_ITEM:
return "vnd.android.cursor.item/vnd.com.example.app.provider.table2";
default:
break;
}
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
}