2026/3/22 1:26:35
网站建设
项目流程
网站开发技术部绩效考核,网站子站点是什么意思,推广系统,建设网站的步骤seo一、通过ContentProvider封装数据1、ContentProvider
ContentProvider为App存取内部数据提供统一的外部接口#xff0c;让不同的应用之间得以共享数据ContentProvider相当于一个窗口、一个门卫
一个应用读取另一个应用的数据#xff0c;比如用户登录时#xff0c;收到验证码…一、通过ContentProvider封装数据1、ContentProviderContentProvider为App存取内部数据提供统一的外部接口让不同的应用之间得以共享数据ContentProvider相当于一个窗口、一个门卫一个应用读取另一个应用的数据比如用户登录时收到验证码自动读取2、ContentProvider案例Client App将用户的输入内容通过ContentProvider跨进程通信传递给Server App3、ContentProvider只是服务端App存取数据的抽象类我们需要在其基础上实现一个完整的内容提供器并重写下列方法1onCreate初始化资源Provider在应用启动的时候就创建了2insert插入数据3delete删除数据4update更新数据5query查询数据6getType获取内容提供器支持的数据类型4、UriUri通用资源标识符Universal Resource Identifer代表数据操作的地址每一个ContentProvider都会有唯一的地址格式content://authority/data_path/id说明1“content://”通用前缀表示该Uri用于ContentProvider定位资源2“authority”授权者名称用来确定具体由哪一个ContentProvider提供资源。因此一般authority都由类的小写全称组成以保证唯一性3“data_path”数据路径用来确定请求的是哪个数据集4id数据编号用来请求单条数据。如果是多条这个字段忽略二、创建Server App1、新建一个Modulechapter07-server2、创建ContentProvider【New】-【Other】-【Content Provider】会自动生成文件3、修改清单文件authorities填生成的Provider文件的包名类名provider android:name.provider.UserInfoProvider android:authoritiescom.example.chapter07_server.provider.UserInfoProvider android:enabledtrue android:exportedtrue /服务端要说明下访问客户端软件包queries package android:namecom.example.chapter07_client / /queries4、UserInfoProvider.javapackage com.example.chapter07_server.provider; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.util.Log; public class UserInfoProvider extends ContentProvider { public UserInfoProvider() { } Override public int delete(Uri uri, String selection, String[] selectionArgs) { Log.d(sam, UserInfoProvider delete); return 0; } 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) { Log.d(sam, UserInfoProvider insert); Log.d(sam, uri uri.toString()); Log.d(sam, values values.toString()); return uri; } Override public boolean onCreate() { Log.d(sam, UserInfoProvider onCreate); return true; } Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.d(sam, UserInfoProvider query); return null; } Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { Log.d(sam, UserInfoProvider update); return 0; } }5、清单文件?xml version1.0 encodingutf-8? manifest xmlns:androidhttp://schemas.android.com/apk/res/android !-- 出于安全考虑Android 11开始要求应用事先说明需要访问的其他软件包 -- queries package android:namecom.example.chapter07_client / /queries application android:allowBackuptrue android:iconmipmap/ic_launcher android:labelstring/app_name android:roundIconmipmap/ic_launcher_round android:supportsRtltrue android:themestyle/Theme.MyApplication provider android:name.provider.UserInfoProvider android:authoritiescom.example.chapter07_server.provider.UserInfoProvider android:enabledtrue android:exportedtrue / activity android:name.MainActivity android:exportedtrue intent-filter action android:nameandroid.intent.action.MAIN / category android:nameandroid.intent.category.LAUNCHER / /intent-filter /activity /application /manifestServer端暴露数据访问接口提供其他Client访问三、通过ContentResolver访问数据1、ContentResolver利用ContentProvider只实现服务端App的数据封装如果客户端App想访问对方的内部数据就要通过内容解析器ContentResolver访问四、创建Client App1、新建一个Modulechapter07-client2、ContentWriteActivity.javapackage com.example.chapter07_client; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; public class ContentWriteActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_name; private EditText et_age; private EditText et_height; private EditText et_weight; private CheckBox ck_married; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_content_write); et_name findViewById(R.id.et_name); et_age findViewById(R.id.et_age); et_height findViewById(R.id.et_height); et_weight findViewById(R.id.et_weight); ck_married findViewById(R.id.ck_married); findViewById(R.id.btn_save).setOnClickListener(this); findViewById(R.id.btn_delete).setOnClickListener(this); findViewById(R.id.btn_update).setOnClickListener(this); findViewById(R.id.btn_query).setOnClickListener(this); } Override public void onClick(View view) { String name et_name.getText().toString(); String age et_age.getText().toString(); String height et_height.getText().toString(); String weight et_weight.getText().toString(); if (view.getId() R.id.btn_save) { ContentValues values new ContentValues(); values.put(name, name); values.put(age, Integer.parseInt(age)); values.put(height, Float.parseFloat(height)); values.put(weight, Float.parseFloat(weight)); values.put(married, ck_married.isChecked()); // 通过ContentResolver访问数据 getContentResolver().insert(UserInfoContent.CONTENT_URI, values); } else if (view.getId() R.id.btn_delete) { } else if (view.getId() R.id.btn_update) { } else if (view.getId() R.id.btn_query) { } } }3、UserInfoContent.javapackage com.example.chapter07_client; import android.net.Uri; public class UserInfoContent { public static final String AUTHORITIES com.example.chapter07_server.provider.UserInfoProvider; // 访问内容提供器的URI // content://com.example.chapter07_server.provider.UserInfoProvider/user public static final Uri CONTENT_URI Uri.parse(content:// AUTHORITIES /user); }4、布局文件activity_content_write.xml?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:apphttp://schemas.android.com/apk/res-auto xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical tools:context.ContentWriteActivity GridLayout android:layout_widthmatch_parent android:layout_heightwrap_content android:columnCount2 android:rowCount4 TextView android:layout_width60dp android:layout_heightwrap_content android:text姓名 android:textSize17sp/ EditText android:idid/et_name android:layout_width0dp android:layout_heightwrap_content android:layout_columnWeight1 android:inputTypetext android:hint请输入姓名/ TextView android:layout_width60dp android:layout_heightwrap_content android:text年龄 android:textSize17sp/ EditText android:idid/et_age android:layout_width0dp android:layout_heightwrap_content android:layout_columnWeight1 android:inputTypenumber android:maxLength3 android:hint请输入年龄/ TextView android:layout_width60dp android:layout_heightwrap_content android:text身高 android:textSize17sp/ EditText android:idid/et_height android:layout_width0dp android:layout_heightwrap_content android:layout_columnWeight1 android:inputTypenumberDecimal android:maxLength6 android:hint请输入身高/ TextView android:layout_width60dp android:layout_heightwrap_content android:text体重 android:textSize17sp/ EditText android:idid/et_weight android:layout_width0dp android:layout_heightwrap_content android:layout_columnWeight1 android:inputTypenumberDecimal android:maxLength6 android:hint请输入体重/ /GridLayout CheckBox android:idid/ck_married android:layout_widthmatch_parent android:layout_heightwrap_content android:text已婚 android:textSize17sp/ Button android:idid/btn_save android:layout_widthmatch_parent android:layout_heightwrap_content android:text添加 android:textSize17sp/ Button android:idid/btn_delete android:layout_widthmatch_parent android:layout_heightwrap_content android:text删除 android:textSize17sp/ Button android:idid/btn_update android:layout_widthmatch_parent android:layout_heightwrap_content android:text修改 android:textSize17sp/ Button android:idid/btn_query android:layout_widthmatch_parent android:layout_heightwrap_content android:text查询 android:textSize17sp/ /LinearLayout5、清单文件出于安全考虑Android 11开始要求应用事先说明需要访问的其他软件包这里服务端也要说明下访问客户端软件包否则一直运行不出来?xml version1.0 encodingutf-8? manifest xmlns:androidhttp://schemas.android.com/apk/res/android queries package android:namecom.example.chapter07_server / /queries application android:allowBackuptrue android:iconmipmap/ic_launcher android:labelstring/app_name android:roundIconmipmap/ic_launcher_round android:supportsRtltrue android:themestyle/Theme.MyApplication activity android:name.ContentWriteActivity android:exportedtrue intent-filter action android:nameandroid.intent.action.MAIN / category android:nameandroid.intent.category.LAUNCHER / /intent-filter /activity /application /manifest6、运行日志以添加数据为例2026-01-12 16:10:28.223 20546-20546 sam com.example.chapter07_server D UserInfoProvider onCreate 2026-01-12 16:11:00.935 20546-20557 sam com.example.chapter07_server D UserInfoProvider insert 2026-01-12 16:11:00.935 20546-20557 sam com.example.chapter07_server D uri content://com.example.chapter07_server.provider.UserInfoProvider/user 2026-01-12 16:11:00.936 20546-20557 sam com.example.chapter07_server D values height170.0 weight56.0 age12 nameqwer marriedtrue