短信备份中添加进度条,同时用传统的布局方法设置进度条,ProgressDialog进度条对话框的进度条实现。最后使用接口的方法实现2个进度条的显示
布局文件activity_advancedTools.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="@style/TitleStyle"
android:text="高级工具"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:text="电话归属地查询"
android:textColor="@color/black"
android:drawableLeft="@android:drawable/ic_menu_camera"
android:gravity="center_vertical"
android:textSize="20sp"
android:drawablePadding="5dp"
android:padding="10dp"
android:clickable="true"
android:onClick="numberAddressQuery"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:text="短信备份"
android:textColor="@color/black"
android:drawableLeft="@android:drawable/ic_menu_camera"
android:gravity="center_vertical"
android:textSize="20sp"
android:drawablePadding="5dp"
android:padding="10dp"
android:clickable="true"
android:onClick="backUpsms"
/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
逻辑文件AdvanceToolsActivity.java
package com.ldw.safe.Activity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.ldw.safe.R;
import com.ldw.safe.utils.SmsUtils;
import com.ldw.safe.utils.SmsUtils.BackUpCallBackSms;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
/*
* 高级工具,包含归属地和短信备份
*/
public class AdvancedToolsActivity extends Activity {
private ProgressDialog pd;
//注解的形式获取对象
@ViewInject(R.id.progressBar1)
private ProgressBar progressBar1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advancedtools);
ViewUtils.inject(this);
}
/*
* 归属地查询
*/
public void numberAddressQuery(View v){
startActivity(new Intent(this, AddressActivity.class));
}
/*
* 短信备份的功能
*/
public void backUpsms(View v){
//*换一种进度条的显示方法,ProgressDialog是对话框的形式,不能改变位置,使用布局的形式显示
pd = new ProgressDialog(AdvancedToolsActivity.this);
pd.setTitle("提示");
pd.setMessage("正在备份信息");
//设置进度条对话框的样式,水平的进度条
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
new Thread(){
public void run() {
//传统方法实现进度条
//boolean result = SmsUtils.backUp(AdvancedToolsActivity.this, pd);
//接口的方法实现进度条
boolean result = SmsUtils.backUp(AdvancedToolsActivity.this, new BackUpCallBackSms(){
@Override
public void befor(int count) {
//方法执行前,初始化2个进度条的最大进度
pd.setMax(count);
progressBar1.setMax(count);
}
@Override
public void onBackUpSms(int process) {
pd.setProgress(process);
progressBar1.setProgress(process);
}
});
if(result){
//子线程不能刷新UI,这种方法子线程弹出Toast
Looper.prepare();
Toast.makeText(AdvancedToolsActivity.this, "备份成功", Toast.LENGTH_SHORT).show();
Looper.loop();
}else{
Looper.prepare();
Toast.makeText(AdvancedToolsActivity.this, "备份失败", Toast.LENGTH_SHORT).show();
Looper.loop();
}
pd.dismiss();
};
}.start();
}
}
smsUtils.java序列化信息的代码
package com.ldw.safe.utils;
import java.io.File;
import java.io.FileOutputStream;
import org.xmlpull.v1.XmlSerializer;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Xml;
/*
* 短信的工具类,用于短信备份
*/
public class SmsUtils {
/**
* 备份短信的接口
*/
public interface BackUpCallBackSms{
//备份短信之前实现的方法
public void befor(int count);
//备份短信中实现的方法
public void onBackUpSms(int process);
}
public static boolean backUp(Context context, BackUpCallBackSms callback){
/*
* 1.先判断是否有sd卡,
* 2.权限是---因此必须使用内容观察者读取
* 3.写短信
*/
//判断sd卡的状态
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//sd卡存在,获取到内容观察者
ContentResolver resolver = context.getContentResolver();
// 获取短信的路径
Uri uri = Uri.parse("content://sms/");
//通过内容观察着,读取短信内容,type:1接受短信,2发送短信
Cursor cursor = resolver.query(uri, new String[]{"address", "date", "type", "body"}, null, null, null);
//短信的总数量
int count = cursor.getCount();
/*
//设置进度条的最大值
pd.setMax(count);
progressBar1.setMax(count);
*/
//初始化进度条默认0
int process = 0;
//接口回掉函数实现
callback.befor(count);
//写文件
try {
//创建保存的短信备份的文件的名字
File file = new File(Environment.getExternalStorageDirectory(), "smsbackup.xml");
FileOutputStream os = new FileOutputStream(file);
//xml序列化器,xml解析是pull解析
XmlSerializer serializer = Xml.newSerializer();
//短信序列化到sd卡,设置编码格式
serializer.setOutput(os, "utf-8");
// standalone表示当前的xml是否是独立文件 ture表示文件独立。yes
serializer.startDocument("utf-8", true);
// 设置开始的节点 第一个参数是命名空间。第二个参数是节点的名字
serializer.startTag(null, "smss");
//设置smss节点上面的属性值 第二个参数是名字。第三个参数是值
serializer.attribute(null, "size", String.valueOf(count));
//读取短信数据
while (cursor.moveToNext()) {
System.err.println("----------------------------");
System.out.println("address = " + cursor.getString(0));
System.out.println("date = " + cursor.getString(1));
System.out.println("type = " + cursor.getString(2));
System.out.println("body = " + cursor.getString(3));
serializer.startTag(null, "sms");
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, "type");
serializer.text(cursor.getString(2));
serializer.endTag(null, "type");
serializer.startTag(null, "body");
serializer.endTag(null, "body");
//读取短信的内容
/**
* 加密:第一个参数表示加密种子(密钥)
* 第二个参数表示加密的内容
*/
serializer.text(Crypto.encrypt("123", cursor.getString(3)));
serializer.endTag(null, "sms");
/*传统方法
//序列化一条process加一
process++;
pd.setProgress(process);
*/
process++;
//接口实现进度条改变
callback.onBackUpSms(process);
//休息200ms
SystemClock.sleep(200);
}
cursor.close();
serializer.endTag(null, "smss");
serializer.endDocument();
os.flush();
os.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
}