网友通过本文主要向大家介绍了contentprovider,contentprovider详解,contentprovider实例,安卓contentprovider,no content provider等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
ContentProvider域名替换小工具,contentprovider域名
开发项目域名想怎么换就怎么换,就是这么任性!
这是一个很有意思的小工具!
这是一个方便开发人员和测试人员的小工具!!
吐槽:
一直在做Android开发,一直总有一个问题存在:做自己公司的apk开发时,线上包和测试包不可兼得~总是在 卸载、安装、卸载、安装。。。的循环操作。很是麻烦,而且另外一个不得不正视的问题就是:只要跟服务端人员进行联调时,就得修改项目中的测试域名,重新打包,也是够麻烦的。最近报名了公司的一个服务,就不得不使用线上包了,被逼无奈想起了这个小设计。
原理:
使用ContentProvider数据共享~
展示图:
设计思路及源码解析:
1.前期准备
a.ContentProvider在android中的作用是对外共享数据, 也就是说你可以通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentProvider对你应用中的数据进行添删改查。
当应用需要通过ContentProvider对外共享数据时,第一步需要继承ContentProvider并重写下面方法:
public class PersonContentProvider extends ContentProvider{ public boolean onCreate() public Uri insert(Uri uri, ContentValues values) public int delete(Uri uri, String selection, String[] selectionArgs) public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) public String getType(Uri uri) }
b.第二步需要在AndroidManifest.xml使用对该ContentProvider进行配置,为了能让其他应用找到该ContentProvider ,ContentProvider采用了authorities(主机名/域名)对它进行唯一标识,你可以把ContentProvider看作是一个网 站(想想,网站也是提供数据者),authorities 就是他的域名
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xc1217.contentprovider">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".activity.UrlListActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<provider android:name=".db.MyContentProvider"
android:authorities="com.xc1217.contentprovider.myprovider"
android:exported="true"/>
</application>
</manifest>
2.数据库设计
/** * Created by ding on 2016/11/15. */ public class DBOpenHelper extends SQLiteOpenHelper { private static final String DBNAME = "1217provider.db"; //数据库名称 private static final int DBVER = 1;//数据库版本 public DBOpenHelper(Context context) { super(context, DBNAME, null, DBVER); } @Override public void onCreate(SQLiteDatabase db) { // id 主键id, url 路径 selected 1 选中,0 未选中 String sql = "CREATE TABLE student (id integer primary key autoincrement, url varchar(500), selected int)"; String sql2 = "CREATE TABLE coach (id integer primary key autoincrement, url varchar(500), selected int)"; db.execSQL(sql);//执行有更改的sql语句 db.execSQL(sql2); initDb(db); } private void initDb(SQLiteDatabase db) { String sql = "INSERT INTO student VALUES (1,'http://www.1217.com/', 0)"; String sq2 = "INSERT INTO student VALUES (2,'http://www.1217.com/', 1)"; String sq3 = "INSERT INTO student VALUES (3,'http://www.1217.com/', 0)"; String sq4 = "INSERT INTO student VALUES (4,'http://www.1217.com/', 0)"; db.execSQL(sql); db.execSQL(sq2); db.execSQL(sq3); db.execSQL(sq4); initAddDbCoach(db); } private void initAddDbCoach(SQLiteDatabase db) { String sql = "INSERT INTO coach VALUES (1,'http://www.1217.com/', 0)"; String sq2 = "INSERT INTO coach VALUES (2,'http://www.1217.com/', 1)"; String sq3 = "INSERT INTO coach VALUES (3,'http://www.1217.com/', 0)"; String sq4 = "INSERT INTO coach VALUES (4,'http://www.1217.com/', 0)"; db.execSQL(sql); db.execSQL(sq2); db.execSQL(sq3); db.execSQL(sq4); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { db.execSQL("DROP TABLE IF EXISTS student"); db.execSQL("DROP TABLE IF EXISTS coach"); onCreate(db); } }
3.继承ContentProvider并重写方法
/** * Created by ding on 2016/11/15. */ public class MyContentProvider extends ContentProvider { private DBOpenHelper dbOpenHelper; //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码 private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); private static final int STUDENTS = 1; private static final int STUDENT = 2; private static final int COACHS = 3; private static final int COACH = 4; static { //如果match()方法匹配content://com.xc1217.contentprovider.myprovider/student路径,返回匹配码为1 MATCHER.addURI("com.xc1217.contentprovider.myprovider", "student", STUDENTS); //如果match()方法匹配content://com.xc1217.contentprovider.myprovider/student/123路径,返回匹配码为2 MATCHER.addURI("com.xc1217.contentprovider.m
您可能想查找下面的文章:
- 使用ContentProvider访问非数据库数据
- Android之ContentProvider数据存储,contentprovider
- ContentProvider域名替换小工具,contentprovider域名
- ContentProvider中央档案馆,以及获取联系人电话的示例,contentprovider
- (试笔)一、Android四大框架之ContentProvider的学习与运用,实现SQLite的增删改查。,安卓sqlite增删改查
- Android中ContentProvider组件数据共享
- 安卓第十四天笔记-内容提供者(ContentProvider),contentprovider