作业说明:
contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;
请自建一个provider,然后在另一个app中使用resolver调用这个provider。
主要代码:
控件界面代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="resolver"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="157dp"
android:layout_height="74dp"
android:text="INSERT"
android:textSize="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.224" />
注册provider
<provider
android:authorities="com.example.homework.MyProvider"
android:name=".MyProvider"
android:enabled="true"
android:exported="true"/>
通过ContentResolver接口提供的方法类访问app1中MyProvider提供的数据
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
private ContentResolver resolver;
//private static final String AUTHORITY='dyt.Provider1';
//private static final Uri NOTIFY_URI=Uri.parse("content://"+AUTHORITY+"/person");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
ContentResolver resolver=getContentResolver();
ContentValues values=new ContentValues();
values.put("name","zhang san");
values.put("age",20);
Integer.parseInt("18");
}
}
截图展示