安卓四大组件之一的ContentProvider
1.内容提供者(ContentProvider)
2.内容访问者(ContentResolver)
一般来说得到系统的URI,我们自己只要写一个内容访问者。但是今天我所讲得是底层做法,自己要写内容提供者和内容访问者。
安卓四大组件都是继承(extends)。我们可以根据内容提供者(ContentProvider)实现两个APP程序之间的操作。下面就是使用内容提供者(ContentProvider),内容访问者(ContentResolver)和其他技术实现的添删改查,利用db文件(可以把它当成数据库)。
一 内容提供者
重写添删改查等方法
1.MyRelationProvider.java文件
package com.example.received_ssm.Provider; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.support.annotation.Nullable; import android.util.Log; import com.example.received_ssm.Helper.SQLHelper; /** * Created by Administrator on 2017/2/6. */ public class MyRelationProvider extends ContentProvider { private SQLiteDatabase sqLiteDatabase; private UriMatcher uriMatcher; @Override public boolean onCreate() { //操作db文件 SQLHelper sqlHelper=new SQLHelper(getContext()); sqLiteDatabase = sqlHelper.openSQLiteDatabase(); //URI匹配器 uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); //URI规则 查询所有 uriMatcher.addURI("com.example.received_ssm.RELAION","relation",1); //URI规则 查询单个 uriMatcher.addURI("com.example.received_ssm.RELAION","relation/#",2); Log.i("test","onCreate"); return false; } //查询(通过匹配器来判断是否是查询所有还是查询单个) @Nullable @Override public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) { Log.i("test","query"); //根据匹配器去匹配 switch (uriMatcher.match(uri)) { case 1://查询所有 return sqLiteDatabase.query(false, "blacklist", strings, s, strings1, null, null, s1, null); case 2://查询单个 long id = ContentUris.parseId(uri); return sqLiteDatabase.query(false, "blacklist", strings, "id=?", new String[]{id+""}, null, null, s1, null); } return null; } @Nullable @Override public String getType(Uri uri) { Log.i("test","insert"); return null; } //添加 @Nullable @Override public Uri insert(Uri uri, ContentValues contentValues) { Log.i("test","insert"); sqLiteDatabase.insert("blacklist",null,contentValues); return null; } //删除 @Override public int delete(Uri uri, String s, String[] strings) { Log.i("test","delete"); return sqLiteDatabase.delete("blacklist", s, strings); } //修改 @Override public int update(Uri uri, ContentValues contentValues, String s, String[] strings) { Log.i("test","update"); return sqLiteDatabase.update("blacklist",contentValues,s,strings); } }
所以内容提供者已经写好 当然具体是操作要根据需求来做 还有清单文件要配置内容提供者信息
2.内容提供者(ContentProvider)APP的清单文件
这里的authorities是唯一的,你也可以当做数据库的主键,name可以写你的内容提供者所以的地址,<provider android:authorities="com.example.received_ssm.RELAION" android:name="com.example.received_ssm.Provider.MyRelationProvider" android:exported="true" ></provider>
exported是设置是否可访问,到了这里内容提供者就写好了,如果是用系统的就不要写内容提供者, 只要写访问者即可。
3.内容访问者(ContentResolver)
其实内容访问者不要写什么 只要得到getContentResolve()得到内容提供者的对象,
例如:ContentResolver resolver= getContentResolver();
就好像是new一个实体类 这个实体类里有添删改查的方法。
到了这里安卓四大组件之一的内容提供者的底层做法已经写好。具体的就是数据库的操作,那就看你们安卓的数据库操作是否学好了package com.example.android24; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ContentResolver resolver; private EditText et_text,et_lxr,xg_id,xg_muber,xg_name; private Uri uri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //访问者 resolver = getContentResolver(); // 查询 删除 //id et_text = (EditText) findViewById(R.id.et_text); //添加 //联系人手机号 et_lxr= (EditText) findViewById(R.id.et_lxr); //修改 //id xg_id= (EditText) findViewById(R.id.xg_id); //手机号码 xg_muber= (EditText) findViewById(R.id.xg_muber); //联系人 xg_name= (EditText) findViewById(R.id.xg_name); } //查询 public void getData(View view){ if (TextUtils.isEmpty(et_text.getText().toString())){ uri = Uri.parse("content://com.example.received_ssm.RELAION/relation"); }else { uri = Uri.parse("content://com.example.received_ssm.RELAION/relation/"+et_text.getText().toString()); } Cursor query = resolver.query(uri, null, null, null, null); while (query.moveToNext()){ int id=query.getInt(0); String name=query.getString(1); String lxr=query.getString(2); Log.i("test",id+" "+name+" "+lxr); } } //删除 public void getdelete(View view){ if (TextUtils.isEmpty(et_text.getText().toString())) { Toast.makeText(this, "为空无法删除!!!", Toast.LENGTH_SHORT).show(); }else { uri=Uri.parse("content://com.example.received_ssm.RELAION"); int delete = resolver.delete(uri, "id=?", new String[]{et_text.getText().toString()}); if (delete>0){ Toast.makeText(this, "删除id为"+et_text.getText().toString()+"成功!!!", Toast.LENGTH_SHORT).show(); } } } //添加 public void getinsert(View view){ if (TextUtils.isEmpty(et_lxr.getText().toString())) { Toast.makeText(this, "为空无法添加!!!", Toast.LENGTH_SHORT).show(); }else { uri=Uri.parse("content://com.example.received_ssm.RELAION"); ContentValues values=new ContentValues(); values.put("name","陌生人"); values.put("number",et_lxr.getText().toString()); resolver.insert(uri, values); } } //修改 public void getupdate(View v){ if (TextUtils.isEmpty(xg_id.getText().toString())) { Toast.makeText(this, "id为空无法修改!!!", Toast.LENGTH_SHORT).show(); }else { if (TextUtils.isEmpty(xg_muber.getText().toString())){ Toast.makeText(this, "手机号码为空无法修改!!!", Toast.LENGTH_SHORT).show(); }else { uri = Uri.parse("content://com.example.received_ssm.RELAION"); ContentValues values = new ContentValues(); if (TextUtils.isEmpty(xg_name.getText().toString())) { values.put("name", "陌生人"); } else { values.put("name", xg_name.getText().toString()); } values.put("number", xg_muber.getText().toString()); int update = resolver.update(uri, values, "id=?", new String[]{xg_id.getText().toString()}); if (update > 0) { Toast.makeText(this, "修改id为" + xg_id.getText().toString() + "成功!!!", Toast.LENGTH_SHORT).show(); } } } } }
。当然我在这里面遇到了一个问题不知道是不是缓存原因,就是写完后有些功能不能实现,害得我找了好久,我把两个APP删掉,重新安装就好了。所以有bug,才有经验。
希望能帮助大家
。。。