从短信备份与还原理解如何降低代码的耦合性
需求:1,实现短信备份还原,2.短信备份还原中的进度条需要可用随时更换,以适应用户要求
v1版本:
/**
- 短信备份
@param view
*/
public static void backUpSms(Context context ,BackCall smsBackCall) {Log.i(TAG, “进入短信备份”);
pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.show(); new Thread(new Runnable() { @Override try { ContentResolver resolver =getContentResolver(); Uri uri =Uri.parse("content://sms/"); Cursor cursor = resolver.query(uri, new String[]{"address","date","body","type"}, null, null, null); XmlSerializer serializer = Xml.newSerializer(); File file = new File(Environment.getExternalStorageDirectory(),"sms.xml"); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "infos"); pd.setMax(cursor.getCount()); int progress=0; while (cursor.moveToNext()) { serializer.startTag(null, "info"); serializer.startTag(null, "address"); serializer.text(cursor.getString(0)); serializer.endTag(null, "address"); serializer.startTag(null, "date"); serializer.text(cursor.getString(1)); serializer.endTag(null, "date"); serializer.startTag(null, "body"); serializer.text(cursor.getString(2)); serializer.endTag(null, "body"); serializer.startTag(null, "type"); serializer.text(cursor.getString(3)); serializer.endTag(null, "type"); serializer.endTag(null, "info"); Thread.sleep(200);//睡眠200毫秒,模拟大量短信 progress++; pd.setProgress(progress); } serializer.endTag(null, "infos"); serializer.endDocument(); fos.close(); pd.dismiss(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start();
}
可见:更新ui的逻辑只有
pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.show(); pd.setMax(cursor.getCount()); pd.setProgress(progress); pd.dismiss();
而且以上代码可能随客户需求变化,而更改的代码
- 比如做成默认选装的进度条
这时候就需要我们重构代码,且让更新ui的逻辑不影响处理数据的逻辑代码
我们将数据逻辑代码,封装为一个方法并暴露一个回调的接口,这个接口用来实现更新ui的方法
v2版:
数据逻辑代码:
接口代码
/**
- 提供一个接口,提供备份,还原的回调函数
*
*/
public interface BackCall{
/**
* 短信备份,还原前的回调方法
* @param max 进度的最大值
*/
public void beforeBackUp(int max);
/**
* 短信备份,还原中回调方法
* @param progress 备份的进度
*/
public void onBackUp(int progress);
}- 提供一个接口,提供备份,还原的回调函数
备份短信的方法
/**- 备份短信的方法
- @param context 上下文
@param smsBackCall 回调接口
*/try {
ContentResolver resolver =context.getContentResolver(); Uri uri =Uri.parse("content://sms/"); Cursor cursor = resolver.query(uri, new String[]{"address","date","body","type"}, null, null, null); File file = new File(Environment.getExternalStorageDirectory(),"sms.xml"); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "infos"); smsBackCall.beforeBackUp(cursor.getCount());//得到结果集的行数 int progress=0; while (cursor.moveToNext()) { serializer.startTag(null, "info"); serializer.startTag(null, "address"); serializer.text(cursor.getString(0)); serializer.endTag(null, "address"); serializer.startTag(null, "date"); serializer.text(cursor.getString(1)); serializer.endTag(null, "date"); serializer.startTag(null, "body"); serializer.text(cursor.getString(2)); serializer.endTag(null, "body"); serializer.startTag(null, "type"); serializer.text(cursor.getString(3)); serializer.endTag(null, "type"); serializer.endTag(null, "info"); Thread.sleep(500); progress++; smsBackCall.onBackUp(progress); } serializer.endTag(null, "infos"); serializer.endDocument(); fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
更新ui的代码
/**- 短信还原按钮绑定的方法
@param view
*/
public void restoreSms(View view) throws Exception{
Log.i(TAG, “进入短信还原”);
pd= new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
new Thread(new Runnable() {@Override public void run() { SmsTools.restoreSms(MainActivity.this, new SmsTools.BackCall() { @Override public void onBackUp(int progress) { pd.setProgress(progress); } @Override public void beforeBackUp(int max) { pd.setMax(max); } }); pd.dismiss(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"短信还原完成",Toast.LENGTH_SHORT).show(); } }); }
}).start();
}