当前位置:   article > 正文

移动开发-使用contentprovider的方法类进行数据获取_android 如何获取三方contentprovider类

android 如何获取三方contentprovider类

设计目标:

1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;

2、自建一个provider,然后在另一个app中使用resolver调用这个provider;

3、理论上需要两个APP进行实验。

功能说明:

1、contentprovider主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性。

2、在本次实验中具体表现为以下效果,在第一个APP:contentprovide中可以切换访问第二个APP:resolver,resolver可以调用contentprovider。

代码展示:

项目provider:

MainActivity.java:

  1. package com.example.provider;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. public class MainActivity extends AppCompatActivity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. MyDAO myDAO=new MyDAO(this);
  10. }
  11. }

MyContentProvider.java:(出现了构成它生命周期的几个方法,包括增、删、改、查、获得类型getType和创建onCreate,只需要在onCreate中传值并new一个MyDAO的方法即可。)

  1. package com.example.provider;
  2. import android.content.ContentProvider;
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. public class MyContentProvider extends ContentProvider {
  8. private MyDAO myDAO;
  9. public MyContentProvider() {
  10. }
  11. @Override
  12. public int delete(Uri uri, String selection, String[] selectionArgs) {
  13. // Implement this to handle requests to delete one or more rows.
  14. throw new UnsupportedOperationException("Not yet implemented");
  15. }
  16. @Override
  17. public String getType(Uri uri) {
  18. // TODO: Implement this to handle requests for the MIME type of the data
  19. // at the given URI.
  20. throw new UnsupportedOperationException("Not yet implemented");
  21. }
  22. @Override
  23. public Uri insert(Uri uri, ContentValues values) {
  24. // TODO: Implement this to handle requests to insert a new row.
  25. //getContext().getContentResolver().insert(uri, values);
  26. return myDAO.DAOinsert(values);
  27. }
  28. @Override
  29. public boolean onCreate() {
  30. // TODO: Implement this to initialize your content provider on startup.
  31. Context context=getContext();
  32. myDAO=new MyDAO(context);
  33. return false;
  34. }
  35. @Override
  36. public Cursor query(Uri uri, String[] projection, String selection,
  37. String[] selectionArgs, String sortOrder) {
  38. // TODO: Implement this to handle query requests from clients.
  39. throw new UnsupportedOperationException("Not yet implemented");
  40. }
  41. @Override
  42. public int update(Uri uri, ContentValues values, String selection,
  43. String[] selectionArgs) {
  44. // TODO: Implement this to handle requests to update one or more rows.
  45. throw new UnsupportedOperationException("Not yet implemented");
  46. }
  47. }

MyDAO.java:(数据处理层,需要在这里写一套真正在数据库上的增删改查。)

  1. package com.example.provider;
  2. import android.content.ContentUris;
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.net.Uri;
  7. public class MyDAO {
  8. private Context context;
  9. private SQLiteDatabase database;
  10. public MyDAO(Context context){
  11. this.context=context;
  12. MyDBhelper dBhelper =new MyDBhelper(context,"JOJODB",null,1);
  13. database=dBhelper.getReadableDatabase();
  14. }
  15. public Uri DAOinsert(ContentValues contentValues){
  16. long rowid=database.insert("student",null,contentValues);
  17. Uri uri=Uri.parse("content://JOJO.provider2/student");
  18. Uri inserturi= ContentUris.withAppendedId(uri,rowid);
  19. context.getContentResolver().notifyChange(inserturi,null);
  20. return inserturi;
  21. }
  22. }

MyDBhelper.java:

  1. package com.example.provider;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. import android.util.Log;
  6. import androidx.annotation.Nullable;
  7. public class MyDBhelper extends SQLiteOpenHelper {
  8. public MyDBhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
  9. super(context, name, factory, version);
  10. Log.d("JOJO","MyDBhelper...");
  11. }
  12. @Override
  13. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  14. sqLiteDatabase.execSQL("create table student(" +
  15. "id integer primary key autoincrement,name varchar,age integer)");
  16. Log.d("JOJO","onCreate...");
  17. }
  18. @Override
  19. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  20. Log.d("JOJO","onUpgrade...");
  21. }
  22. }

Manifest.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. package="com.example.provider">
  5. <application
  6. android:allowBackup="true"
  7. android:dataExtractionRules="@xml/data_extraction_rules"
  8. android:fullBackupContent="@xml/backup_rules"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:roundIcon="@mipmap/ic_launcher_round"
  12. android:supportsRtl="true"
  13. android:theme="@style/Theme.Provider"
  14. tools:targetApi="31">
  15. <provider
  16. android:name=".MyContentProvider"
  17. android:authorities="JOJO.provider2"
  18. android:enabled="true"
  19. android:exported="true"></provider>
  20. <activity
  21. android:name=".MainActivity"
  22. android:exported="true">
  23. <intent-filter>
  24. <action android:name="android.intent.action.MAIN" />
  25. <category android:name="android.intent.category.LAUNCHER" />
  26. </intent-filter>
  27. </activity>
  28. </application>
  29. </manifest>

resovler项目:

MainActivity.java:

  1. package com.example.resovler;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.ContentResolver;
  4. import android.content.ContentValues;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. ContentResolver resolver=getContentResolver();
  15. ContentValues values=new ContentValues();
  16. values.put("name","JOJO");
  17. values.put("age",20);
  18. Uri uri=Uri.parse("content://JOJO.provider2/student");
  19. Button button = findViewById(R.id.button);
  20. button.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View view) {
  23. resolver.insert(uri,values);
  24. }
  25. });
  26. }
  27. }

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="resovler"
  12. android:textSize="45dp"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintEnd_toEndOf="parent"
  15. app:layout_constraintHorizontal_bias="0.526"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent"
  18. app:layout_constraintVertical_bias="0.363" />
  19. <Button
  20. android:id="@+id/button"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="insert"
  24. android:textSize="45dp"
  25. app:layout_constraintBottom_toBottomOf="parent"
  26. app:layout_constraintEnd_toEndOf="parent"
  27. app:layout_constraintHorizontal_bias="0.555"
  28. app:layout_constraintStart_toStartOf="parent"
  29. app:layout_constraintTop_toTopOf="parent"
  30. app:layout_constraintVertical_bias="0.587" />
  31. </androidx.constraintlayout.widget.ConstraintLayout>

效果展示:

运行进入resovler项目界面

点击INSETR按钮

在provider项目的数据库中增添信息

源码仓库: Lzr2002/LZR (github.com)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/245837
推荐阅读
相关标签
  

闽ICP备14008679号