使用内容提供器是Android实现跨程序共享数据的标准方式。
如果我们想要实现拨号功能,就必须获得权限,因为拨打电话涉及用户手机资费问题,在AndroidManifest中声明CALL_PHONE权限后,若为Android6.0以下则可以正常拨打电话,而在Android6.0以上则需要运行时权限才能拨打电话,运行时权限是用户是否有授权,授权的方式是会弹出一个权限申请对话框,若用户选择同意则才能正常运行。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button makeCall = findViewById(R.id.make_call);
makeCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){//判断是否用户已授权
ActivityCompat.requestPermissions(MainActivity.this,new String[] {Manifest.permission.CALL_PHONE},1);//若没授权则弹出权限申请对话框
}else{//若已授权则直接拨号
call();
}
}
});
}
private void call(){
try {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}catch (SecurityException e){
e.printStackTrace();
}
}
//权限申请对话框选择后的回调方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){//用户同意授权
call();
}else {
Toast.makeText(this,"You denied the permission",Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
}
如果我们想获取手机系统联系人的数据的话,则需要使用ContentResolver。
新建ListView来显示联系人的数据,activity.main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/contacts_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
ListView>
LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ArrayAdapter<String > adapter;
List<String> contactsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView contactsView = findViewById(R.id.contacts_view);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactsList);
contactsView.setAdapter(adapter);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.READ_CONTACTS},1);
}else{
readContacts();
}
}
private void readContacts(){
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));
contactsList.add(displayName + "\n" + number);
}
adapter.notifyDataSetChanged();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(cursor != null){
cursor.close();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
readContacts();
}else {
Toast.makeText(this,"You denied the permission",Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
}
最后不要忘了在AndroidManifest中添加权限声明:
<uses-permission android:name="android.permission.READ_CONTACTS">uses-permission>
如果想实现程序的数据共享则需要使用ContentProvider和ContentResolver,例如我们想新建一个项目来对之前学习数据库存储时的BookStore.db数据库进行增删改查。
首先我们需要打开之前建立数据库的项目并且新建Content Provider并命名为DatabaseProvider:
public class DatabaseProvider extends ContentProvider {
public static final int BOOK_DIR = 0;
public static final int BOOK_ITEM = 1;
public static final int CATEGORY_DIR = 2;
public static final int CATEGORY_ITEM = 3;
public static final String AUTHORITY = "com.example.youyu4.databasetest.provider";
private static UriMatcher uriMatcher;
private MyDatabaseHelper dbHelper;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"book",BOOK_DIR);
uriMatcher.addURI(AUTHORITY,"book/#",BOOK_ITEM);
uriMatcher.addURI(AUTHORITY,"category",CATEGORY_DIR);
uriMatcher.addURI(AUTHORITY,"category/#",CATEGORY_ITEM);
}
@Override
public boolean onCreate() {
dbHelper = new MyDatabaseHelper(getContext(),"BookStore.db",null,2);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
//查询数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
cursor = db.query("Book",projection,selection,selectionArgs,null,null,sortOrder);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
cursor = db.query("Book",projection,"id = ?",new String[] {bookId},null,null,sortOrder);
break;
case CATEGORY_DIR:
cursor = db.query("Category",projection,selection,selectionArgs,null,null,sortOrder);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
cursor = db.query("Category",projection,"id = ?",new String[] {categoryId},null,null,sortOrder);
break;
default:
break;
}
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
//添加数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
Uri uriReturn = null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
case BOOK_ITEM:
long newBookId = db.insert("Book",null,values);
uriReturn = Uri.parse("content://" + AUTHORITY + "/book/" + newBookId);
break;
case CATEGORY_DIR:
case CATEGORY_ITEM:
long newCategoryId = db.insert("Category",null,values);
uriReturn = Uri.parse("content://" + AUTHORITY + "/category/" + newCategoryId);
break;
default:
break;
}
return uriReturn;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
//更新数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
int updatedRows = 0;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
updatedRows = db.update("Book",values,selection,selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
updatedRows = db.update("Book",values,"id = ?",new String[] {bookId});
break;
case CATEGORY_DIR:
updatedRows = db.update("Category",values,selection,selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
updatedRows = db.update("Category",values,"id = ?",new String[] {categoryId});
break;
default:
break;
}
return updatedRows;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
//删除数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
int deletedRows = 0;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
deletedRows = db.delete("Book",selection,selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
deletedRows = db.delete("Book","id = ?",new String[] {bookId});
break;
case CATEGORY_DIR:
deletedRows = db.delete("Category",selection,selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
deletedRows = db.delete("Category","id = ?",new String[] {categoryId});
break;
default:
break;
}
return deletedRows;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.example.youyu4.databasetest.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.example.youyu4.databasetest.provider.book";
case CATEGORY_DIR:
return "vnd.android.cursor.dir/vnd.com.example.youyu4.databasetest.provider.category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.com.example.youyu4.databasetest.provider.category";
}
return null;
}
}
新建完contentprovider后我们新建一个新的项目并布局4个按钮用于对上面数据库项目中的数据库数据进行增删改查,主要原理是使用Uri.parse()方法将内容URI解析成Uri对象,然后调用ContentResolver中的增删改查方法执行数据库操作。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private String newId;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addData = findViewById(R.id.add_data);
Button queryData = findViewById(R.id.query_data);
Button updateData = findViewById(R.id.update_data);
Button deleteData = findViewById(R.id.delete_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//添加数据
Uri uri = Uri.parse("content://com.example.youyu4.databasetest.provider/book");
ContentValues values = new ContentValues();
values.put("name","abc");
values.put("author","sjlqmt");
values.put("pages",1234);
values.put("price",55.55);
Uri newUri = getContentResolver().insert(uri,values);
newId = newUri.getPathSegments().get(1);
}
});
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//查询数据
Uri uri = Uri.parse("content://com.example.youyu4.databasetest.provider/book");
Cursor cursor = getContentResolver().query(uri,null,null,null,null);
if(cursor != null){
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d(TAG, "book name is " + name);
Log.d(TAG, "book author is " + author);
Log.d(TAG, "book pages is " + pages);
Log.d(TAG, "book price is " + price);
}
cursor.close();
}
}
});
updateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//更新数据
Uri uri = Uri.parse("content://com.example.youyu4.databasetest.provider/book/" + newId);
ContentValues values = new ContentValues();
values.put("name","qqq");
values.put("pages",5678);
values.put("price",999.99);
getContentResolver().update(uri,values,null,null);
}
});
deleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//删除数据
Uri uri = Uri.parse("content://com.example.youyu4.databasetest.provider/book/" + newId);
getContentResolver().delete(uri,null,null);
}
});
}
}