最新实战教程,让你了解Android自动化刷量、作弊与防作弊的那些事,案例:刷友盟统计、批量注册苹果帐号
好,我们上一次讲了那个短信的备份的,既然有备份,那肯定也要有个还原的啦,今天我们就把那个还原的做一下
其实很简单的,就是把我们上一次备份的xml解析出来,然后写回到数据库里面而已
我们现在就开始写代码,先在上一次的com.xiaobin.security.engine.SmsService里面增加一个方法
package com.xiaobin.security.engine;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Xml;
import com.xiaobin.security.domain.SmsInfo;
public class SmsService
{
private Context context;
public SmsService(Context context)
{
this.context = context;
}
public List<SmsInfo> getSmsInfo()
{
List<SmsInfo> infos = new ArrayList<SmsInfo>();
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
Cursor cursor = resolver.query(uri, new String[] {"_id", "address", "date", "type", "body"}, null, null, " date desc ");
SmsInfo info;
while(cursor.moveToNext())
{
info = new SmsInfo();
String id = cursor.getString(0);
String address = cursor.getString(1);
String date = cursor.getString(2);
int type = cursor.getInt(3);
String body = cursor.getString(4);
info.setId(id);
info.setAddress(address);
info.setDate(date);
info.setType(type);
info.setBody(body);
infos.add(info);
}
cursor.close();
return infos;
}
//还原短信 path为文件路径
public void restore(String path, ProgressDialog pd) throws Exception
{
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
XmlPullParser parser = Xml.newPullParser();
ContentValues values = null;
parser.setInput(fis, "utf-8");
int type = parser.getEventType();
int index = 0;
while(type != XmlPullParser.END_DOCUMENT)
{
switch(type)
{
case XmlPullParser.START_TAG :
if("count".equals(parser.getName()))
{
int count = Integer.parseInt(parser.nextText());
pd.setMax(count);
}
if("sms".equals(parser.getName()))
{
values = new ContentValues();
}
else if("address".equals(parser.getName()))
{
values.put("address", parser.nextText());
}
else if("date".equals(parser.getName()))
{
values.put("date", parser.nextText());
}
else if("type".equals(parser.getName()))
{
values.put("type", parser.nextText());
}
else if("body".equals(parser.getName()))
{
values.put("body", parser.nextText());
}
break;
case XmlPullParser.END_TAG :
if("sms".equals(parser.getName()))
{
ContentResolver resolver = context.getContentResolver();
resolver.insert(Uri.parse("content://sms/"), values);
values = null;
index++;
pd.setProgress(index);
}
break;
default :
break;
}
type = parser.next();
}
}
}
因为恢复短信这个操作是非常重要的,如果恢复有问题,那么就有可能造成用户的数据丢失了,所以我们加了一个对话框,让用户不用取消,直到恢复完成为止
现在恢复短信的方法就写好啦,我们现在就要回到com.xiaobin.security.ui.AToolActivity这个类里面了,为短信恢复这个条目添加相应的事件还有方法啦
因为短信恢复是一个非常耗时的操作,所以我们就开启一个线程来运行它的
com.xiaobin.security.ui.AToolActivity
package com.xiaobin.security.ui;
import java.io.File;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.xiaobin.security.R;
import com.xiaobin.security.engine.DownloadTask;
import com.xiaobin.security.engine.SmsService;
import com.xiaobin.security.service.AddressService;
import com.xiaobin.security.service.BackupSmsService;
public class AToolActivity extends Activity implements OnClickListener
{
private static final int ERROR = 0;
private static final int SUCCESS = 1;
private TextView tv_atool_query;
private TextView tv_atool_number_service_state;
private CheckBox cb_atool_state;
private TextView tv_atool_select_bg;
private TextView tv_atool_change_location;
private TextView tv_atool_number_security;
private TextView tv_atool_sms_backup;
private TextView tv_atool_sms_restore;
private Intent serviceIntent;
private ProgressDialog pd;
private SharedPreferences sp;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case ERROR :
Toast.makeText(AToolActivity.this, "下载数据库失败,请检查网络!", Toast.LENGTH_SHORT).show();
break;
case SUCCESS :
Toast.makeText(AToolActivity.this, "数据库下载成功!", Toast.LENGTH_SHORT).show();
break;
default :
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.atool);
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
tv_atool_query = (TextView) findViewById(R.id.tv_atool_query);
tv_atool_query.setOnClickListener(this);
tv_atool_select_bg = (TextView) findViewById(R.id.tv_atool_select_bg);
tv_atool_select_bg.setOnClickListener(this);
tv_atool_change_location = (TextView) findViewById(R.id.tv_atool_change_location);
tv_atool_change_location.setOnClickListener(this);
tv_atool_number_security = (TextView) findViewById(R.id.tv_atool_number_security);
tv_atool_number_security.setOnClickListener(this);
tv_atool_sms_backup = (TextView) findViewById(R.id.tv_atool_sms_backup);
tv_atool_sms_backup.setOnClickListener(this);
tv_atool_sms_restore = (TextView) findViewById(R.id.tv_atool_sms_restore);
tv_atool_sms_restore.setOnClickListener(this);
tv_atool_number_service_state = (TextView) findViewById(R.id.tv_atool_number_service_state);
cb_atool_state = (CheckBox) findViewById(R.id.cb_atool_state);
serviceIntent = new Intent(this, AddressService.class);
cb_atool_state.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
startService(serviceIntent);
tv_atool_number_service_state.setTextColor(Color.BLACK);
tv_atool_number_service_state.setText("归属地服务已开启");
}
else
{
stopService(serviceIntent);
tv_atool_number_service_state.setTextColor(Color.RED);
tv_atool_number_service_state.setText(R.string.number_service_state);
}
}
});
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.tv_atool_query :
query();
break;
case R.id.tv_atool_select_bg :
selectStyle();
break;
case R.id.tv_atool_change_location :
Intent intent = new Intent(this, DragViewActivity.class);
startActivity(intent);
break;
case R.id.tv_atool_number_security :
Intent i = new Intent(this, NumberSecurityActivity.class);
startActivity(i);
break;
case R.id.tv_atool_sms_backup :
Intent backupIntent = new Intent(this, BackupSmsService.class);
startService(backupIntent);
break;
case R.id.tv_atool_sms_restore :
restore();
break;
default :
break;
}
}
private void query()
{
if(isDBExist())
{
Intent intent = new Intent(this, QueryNumberActivity.class);
startActivity(intent);
}
else
{
//提示用户下载数据库
pd = new ProgressDialog(this);
pd.setMessage("正在下载数据库...");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(false);
pd.show();
new Thread()
{
public void run()
{
String path = getResources().getString(R.string.serverdb);
File dir = new File(Environment.getExternalStorageDirectory(), "/security/db");
if(!dir.exists())
{
dir.mkdirs();
}
String dbPath = Environment.getExternalStorageDirectory() + "/security/db/data.db";
try
{
//这个类,我们在做更新apk的时候已经写好的啦,现在直接拿过来用就可以啦
DownloadTask.getFile(path, dbPath, pd);
pd.dismiss();
}
catch (Exception e)
{
e.printStackTrace();
pd.dismiss();
Message message = new Message();
message.what = ERROR;
handler.sendMessage(message);
}
};
}.start();
}
}
private boolean isDBExist()
{
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File file = new File(Environment.getExternalStorageDirectory() + "/security/db/data.db");
if(file.exists())
{
return true;
}
}
return false;
}
private void selectStyle()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("归属地显示风格");
String[] items = new String[] {"半透明", "活力橙", "苹果绿", "孔雀蓝", "金属灰"};
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Editor editor = sp.edit();
editor.putInt("background", which);
editor.commit();
}
});
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
builder.create().show();
}
private void restore()
{
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("还原短信");
pd.setMessage("正在还原短信...");
pd.setCancelable(false);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
final SmsService smsService = new SmsService(this);
new Thread()
{
public void run()
{
try
{
smsService.restore(Environment.getExternalStorageDirectory() + "/security/backup/smsbackup.xml", pd);
pd.dismiss();
Looper.prepare();//创建一个Looper
Toast.makeText(getApplicationContext(), "还原成功", Toast.LENGTH_SHORT).show();
Looper.loop();//轮循一次Looper
}
catch (Exception e)
{
e.printStackTrace();
Looper.prepare();//创建一个Looper
Toast.makeText(getApplicationContext(), "还原失败", Toast.LENGTH_SHORT).show();
Looper.loop();//轮循一次Looper
}
}
}.start();
}
}
好啦,恢复短信的操作到这里,也完成啦