你的位置:首页 > 操作系统

[操作系统]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.

 

<?<manifest      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.myprovider", "student/#", STUDENT);//#号为通配符    MATCHER.addURI("com.xc1217.contentprovider.myprovider", "coach", COACHS);    MATCHER.addURI("com.xc1217.contentprovider.myprovider", "coach/#", COACH);//#号为通配符  }  @Override  public int delete(Uri uri, String selection, String[] selectionArgs) {    SQLiteDatabase db = dbOpenHelper.getWritableDatabase();    int count = 0;    long id = 0;    String where = "";    switch (MATCHER.match(uri)) {      case STUDENTS:        count = db.delete("student", selection, selectionArgs);        return count;      case STUDENT:        //ContentUris类用于获取Uri路径后面的ID部分        id = ContentUris.parseId(uri);        where = "id = " + id;        if (selection != null && !"".equals(selection)) {          where = selection + " and " + where;        }        count = db.delete("student", where, selectionArgs);        return count;      case COACHS:        count = db.delete("coach", selection, selectionArgs);        return count;      case COACH:        //ContentUris类用于获取Uri路径后面的ID部分        id = ContentUris.parseId(uri);        where = "id = " + id;        if (selection != null && !"".equals(selection)) {          where = selection + " and " + where;        }        count = db.delete("coach", where, selectionArgs);        return count;      default:        throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());    }  }  /**   * 该方法用于返回当前Url所代表数据的MIME类型。   * 如果操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir/开头   * 如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头   */  @Override  public String getType(Uri uri) {    switch (MATCHER.match(uri)) {      case STUDENTS:        return "vnd.android.cursor.dir/student";      case STUDENT:        return "vnd.android.cursor.item/student";      case COACHS:        return "vnd.android.cursor.dir/coach";      case COACH:        return "vnd.android.cursor.item/coach";      default:        throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());    }  }  @Override  public Uri insert(Uri uri, ContentValues values) {    SQLiteDatabase db = dbOpenHelper.getWritableDatabase();    long rowid = 0;    Uri insertUri = null;//得到代表新增记录的Uri    switch (MATCHER.match(uri)) {      case STUDENTS:        rowid = db.insert("student", "url", values);        insertUri = ContentUris.withAppendedId(uri, rowid);//得到代表新增记录的Uri        this.getContext().getContentResolver().notifyChange(uri, null);        return insertUri;      case COACHS:        rowid = db.insert("coach", "url", values);        insertUri = ContentUris.withAppendedId(uri, rowid);//得到代表新增记录的Uri        this.getContext().getContentResolver().notifyChange(uri, null);        return insertUri;      default:        throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());    }  }  @Override  public boolean onCreate() {    this.dbOpenHelper = new DBOpenHelper(this.getContext());    return false;  }  @Override  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,            String sortOrder) {    SQLiteDatabase db = dbOpenHelper.getReadableDatabase();    long id = 0;    String where = null;    switch (MATCHER.match(uri)) {      case STUDENTS:        return db.query("student", projection, selection, selectionArgs, null, null, sortOrder);      case STUDENT:        id = ContentUris.parseId(uri);        where = "id = " + id;        if (selection != null && !"".equals(selection)) {          where = selection + " and " + where;        }        return db.query("student", projection, where, selectionArgs, null, null, sortOrder);      case COACHS:        return db.query("coach", projection, selection, selectionArgs, null, null, sortOrder);      case COACH:        id = ContentUris.parseId(uri);        where = "id = " + id;        if (selection != null && !"".equals(selection)) {          where = selection + " and " + where;        }        return db.query("coach", projection, where, selectionArgs, null, null, sortOrder);      default:        throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());    }  }  @Override  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {    SQLiteDatabase db = dbOpenHelper.getWritableDatabase();    int count = 0;    long id = 0;    String where = null;    switch (MATCHER.match(uri)) {      case STUDENTS:        count = db.update("student", values, selection, selectionArgs);        return count;      case STUDENT:        id = ContentUris.parseId(uri);        where = "id = " + id;        if (selection != null && !"".equals(selection)) {          where = selection + " and " + where;        }        count = db.update("student", values, where, selectionArgs);        return count;      case COACHS:        count = db.update("coach", values, selection, selectionArgs);        return count;      case COACH:        id = ContentUris.parseId(uri);        where = "id = " + id;        if (selection != null && !"".equals(selection)) {          where = selection + " and " + where;        }        count = db.update("coach", values, where, selectionArgs);        return count;      default:        throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());    }  }}

 

4.MainActivity. java
public class MainActivity extends AppCompatActivity {  private static final String TAG = "MainActivity";  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);    setSupportActionBar(toolbar);    findViewById(R.id.lay_student).setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        Intent intent = new Intent(MainActivity.this, UrlListActivity.class);        intent.putExtra("table", "student");        startActivity(intent);      }    });    findViewById(R.id.lay_coach).setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        Intent intent = new Intent(MainActivity.this, UrlListActivity.class);        intent.putExtra("table", "coach");        startActivity(intent);      }    });  }}

5.UrlListActivity.java
public class UrlListActivity extends AppCompatActivity {  private static final String TAG = "UrlListActivity";  String uriString = "content://com.xc1217.contentprovider.myprovider/";  private String table = "";  private List<UrlBean> mList = new ArrayList<UrlBean>();  private ListView listView;  private int position;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_url_list);    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);    setSupportActionBar(toolbar);    Intent intent = getIntent();    if (intent != null) {      table = intent.getStringExtra("table");      if (TextUtils.isEmpty(table))        return;      setTitle(table);      uriString = uriString + table;    }    initUI();    getUrlList();  }  private void initUI() {    listView = (ListView) findViewById(R.id.list_url);    findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        try {          showAddPop();        } catch (Throwable throwable) {          throwable.printStackTrace();        }      }    });    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {      @Override      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {        position = i;        showClickPop();      }    });  }  private void showAddPop() {    final EditText editText = new EditText(this);    editText.setText("http://www.1217.com/");    editText.setHint("http://www.1217.com/");    final AlertDialog alertDialog = new AlertDialog.Builder(this).        setTitle("添加")        .setNegativeButton("取消", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub          }        })        .setPositiveButton("确定", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            String url = editText.getText().toString();            insertUrl(url);          }        }).setView(editText).create();    alertDialog.show();    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View        .OnClickListener() {      @Override      public void onClick(View v) {        String url = editText.getText().toString();        if (TextUtils.isEmpty(url)) {          Toast.makeText(UrlListActivity.this, "不能为空", Toast.LENGTH_SHORT).show();          return;        }        insertUrl(url);        alertDialog.dismiss();        getUrlList();      }    });  }  private void showClickPop() {    final String[] arrayFruit = new String[]{"选择", "删除"};    Dialog alertDialog = new AlertDialog.Builder(this).        setTitle("编辑")        .setItems(arrayFruit, new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            switch (which) {              case 0:                for (int i = 0; i < mList.size(); i++) {                  UrlBean urlBean = mList.get(i);                  urlBean.selected = (position == i ? 1 : 0);                  update(urlBean);                }                UrlAdapter urlAdapter = new UrlAdapter(UrlListActivity.this, mList);                listView.setAdapter(urlAdapter);                break;              case 1:                int id = mList.get(position).id;                deleteById(id);                getUrlList();                break;              default:                break;            }          }        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub          }        }).            create();    alertDialog.show();  }  //往内容提供者添加数据  public void insertUrl(String url) {    try {      ContentResolver contentResolver = this.getContentResolver();      Uri insertUri = Uri.parse(uriString);      ContentValues values = new ContentValues();      values.put("url", url);      values.put("selected", 0);      Uri uri = contentResolver.insert(insertUri, values);      Log.i(TAG, uri.toString());    } catch (Exception e) {      e.printStackTrace();    }  }  //更新内容提供者中的数据  public void update(UrlBean urlBean) {    try {      ContentResolver contentResolver = this.getContentResolver();      Uri updateUri = Uri.parse(uriString + "/" + urlBean.id);      ContentValues values = new ContentValues();      values.put("url", urlBean.url);      values.put("selected", urlBean.selected);      contentResolver.update(updateUri, values, null, null);    } catch (Exception e) {      e.printStackTrace();    }  }  //从内容提供者中删除数据  public void deleteById(Integer id) {    try {      ContentResolver contentResolver = this.getContentResolver();      Uri deleteUri = Uri.parse(uriString + "/" + id);      contentResolver.delete(deleteUri, null, null);    } catch (Exception e) {      e.printStackTrace();    }  }  //获取内容提供者中的数据  public void getUrlList() {    try {      ContentResolver contentResolver = this.getContentResolver();      Uri selectUri = Uri.parse(uriString);      Cursor cursor = contentResolver.query(selectUri, null, null, null, null);      if (cursor == null)        return;      mList.clear();      while (cursor.moveToNext()) {        int id = cursor.getInt(cursor.getColumnIndex("id"));        String url = cursor.getString(cursor.getColumnIndex("url"));        int selected = cursor.getInt(cursor.getColumnIndex("selected"));        Log.i(TAG, "id=" + id + ",url=" + url + ",selected=" + selected);        UrlBean urlBean = new UrlBean(id, url, selected);        mList.add(urlBean);      }      UrlAdapter urlAdapter = new UrlAdapter(this, mList);      listView.setAdapter(urlAdapter);    } catch (Exception e) {      e.printStackTrace();    }  }}

6.UrlAdapter.java
public class UrlAdapter extends BaseAdapter {  private Context mContext;  private List<UrlBean> mList;  public UrlAdapter(Context c, List<UrlBean> l) {    // TODO Auto-generated constructor stub    mContext = c;    mList = l;  }  @Override  public int getCount() {    // TODO Auto-generated method stub    return mList.size();  }  @Override  public Object getItem(int arg0) {    // TODO Auto-generated method stub    return mList.get(arg0);  }  @Override  public long getItemId(int arg0) {    // TODO Auto-generated method stub    return arg0;  }  @Override  public View getView(final int i, View view, ViewGroup arg2) {    ViewHoldel holdel;    if (view == null) {      holdel = new ViewHoldel();      view = LayoutInflater.from(mContext).inflate(R.layout.url_item,          null);      holdel.tvContent = (TextView) view.findViewById(R.id.tv_content);      holdel.imageView = (ImageView) view.findViewById(R.id.imv_right);      view.setTag(holdel);    } else {      holdel = (ViewHoldel) view.getTag();    }    holdel.tvContent.setText(mList.get(i).url);    if (mList.get(i).selected == 1) {      holdel.imageView.setVisibility(View.VISIBLE);    } else {      holdel.imageView.setVisibility(View.GONE);    }    return view;  }  class ViewHoldel {    TextView tvContent;    ImageView imageView;  }}

7.url_item.
<?

  

重点来了!!!

开发人员接入代码,替换域名:

代码加入到 Application   初始化方法中;根据需要修改下代码即可。

private void changeServerHostFromProvider() {  Uri uri = Uri.parse("content://com.xc1217.contentprovider.myprovider/student");  Cursor cursor = getContentResolver().query(uri, null, null, null, null);  if (cursor != null) {    while (cursor.moveToNext()) {      String url = cursor.getString(cursor.getColumnIndex("url"));      int selected = cursor.getInt(cursor.getColumnIndex("selected"));// 1 选中,否则未选中      if (selected == 1 && !TextUtils.isEmpty(url)) {        Urls.BASE_URL_TEST = url;        Urls.BASE_URL_ONLINE = url;                return;      }    }    cursor.close();  }}