1:
ContentProvider-基础应用
定义:ContentProvider是操作其他应用程序内容的组件。
Uri phoneUri =ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(phoneUri,new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER},null,null,null);
while (cursor.moveToNext()){
}
获取联系人的Uri
DISPLAY_NAME和NUMBER:联系人的昵称和手机号
Uri uri= Uri.parse("content://sms/");
Cursor cursor=cr.query(uri,null,null,null,null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("body"));
}
cursor.close();
content://sms/:获取短信的Uri
body:消息的列的索引
Intent image = new Intent(Intent.ACTION_PICK); image.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(image, 100);
"image/*:获取图片的Uri
startActivityForResult:带回传的跳转
2:
自定义内容提供者
1、创建ContentProvider,继承ContentProvider
public class MyContentProvider extends ContentProvider {
//使用sqlite数据库
}
2、重写onCreate(),insert(),select(),update(),delete(),getType()方法
3、注册内容提供者的uri
static {
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "person", CODE_PERSON);
}
AUTHORITY:对外暴露该应用的标识,是个自定义的字符串
CODE_PERSON:自定义的int码
4、清单文件中配置provider
<provider android:name="com.example.mycontentprovider.MyContentProvider" android:authorities="com.example.mycontentprovider.provider" android:exported="true" >
</provider>
name:ContentProvider的名字
name:ContentProvider的名字
exported:是否允许其他应用访问
String AUTHORITIES = "com.example.mycontentprovider.provider";
String uri = "content://" + AUTHORITIES + "/person";
Cursor cursor = cr.query(Uri.parse(uri), null, null, null, null);
while (cursor.moveToNext()) {
int _id = cursor.getInt(0);
}
AUTHORITIES:清单文件里配置的authorities
person:自定义类中URI的第二个参数
3;
自定义内容提供者-案例
1、创建ContentProvider,继承ContentProvider
2、重写onCreate(),insert(),select(),update(),delete(),getType()方法
public class MyContentProvider extends ContentProvider {
MyOpenHelper openHelper;
SQLiteDatabase database;
/**
* 获得数据库
* @return
*/
@Override
public boolean onCreate() {
openHelper = new MyOpenHelper(getContext());
database = openHelper.getWritableDatabase();
if(database != null)
return true;
else
return false;
}
@Nullable
@Override
public Cursor query( @NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return database.query("user",projection,selection,selectionArgs,null,null,sortOrder);
}
/**
* 返回是一条数据还是多条数据
* @param uri
* @return
*/
@Nullable
@Override
public String getType( @NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert( @NonNull Uri uri, @Nullable ContentValues values) {
database.insert("user",null,values);
return uri;
}
@Override
public int delete( @NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
return database.delete("user",selection,selectionArgs);
}
@Override
public int update( @NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return database.update("user",values,selection,selectionArgs);
}
}
3、清单文件注册内容提供者
<provider
android:authorities="com.bawei.1804A"
android:name=".MyContentProvider"
android:exported="true"/>
public class MainActivity extends AppCompatActivity {
Uri uri;
ContentResolver resolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uri = Uri.parse("content://com.bawei.1804A");
resolver = getContentResolver();
}
public void read(View view) {
ContentValues values = new ContentValues();
values.put("uname","ck");
resolver.insert(uri,values);
}
public void updateUser(View view) {
ContentValues values = new ContentValues();
values.put("uname","LV");
values.put("age",10);
values.put("sex","女");
values.put("birthday",new Date().toString());
resolver.update(uri,values,"uname = ?",new String[]{"ck"}); }
public void deleteUser(View view) {
resolver.delete(uri,null,null); }
public void queryUser(View view) {
Cursor cursor = resolver.query(uri,null,null,null,null);
while (cursor.moveToNext()){
int uid = cursor.getInt(cursor.getColumnIndex("uid"));
String uname = cursor.getString(cursor.getColumnIndex("uname"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
String birthday = cursor.getString(cursor.getColumnIndex("birthday"));
Log.d("amy",uid+"-"+uname+"-"+age+"-"+sex+"-"+birthday); } }}
4:
自定义权限
1、修改内容提供方的清单文件
自定义权限+内容提供者注册的时候使用权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my_contentprovider">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册内容提供者 exported能否被其他应用程序访问(重要) authorities:uri(全小写) name:类的全路径-->
<provider
android:readPermission="com.bawei.permission.READ"
android:writePermission="com.bawei.permission.WRITE"
android:exported="true"
android:authorities="com.bawei"
android:name=".MyProvider"></provider>
</application>
<!--1.自定义权限 (注意位置,没有在application里面)-->
<permission
android:name="com.bawei.permission.READ"
android:protectionLevel="normal">
</permission>
<permission
android:name="com.bawei.permission.WRITE"
android:protectionLevel="normal" >
</permission>
</manifest>
<!--注册内容提供者 exported能否被其他应用程序访问(重要) authorities:uri(全小写) name:类的全路径-->
<provider
android:readPermission="com.bawei.permission.READ"
android:writePermission="com.bawei.permission.WRITE"
android:exported="true"
android:authorities="com.bawei"
android:name=".MyProvider"></provider>
</application>
1、内容解析方清单文件添加权限
<uses-permission android:name="com.bawei.permission.WRITE"></uses-permission>
<uses-permission android:name="com.bawei.permission.READ"></uses-permission>