Android項目實戰--手機衛士19--簡訊的恢複

來源:互聯網
上載者:User

好,我們上一次講了那個簡訊的備份的,既然有備份,那肯定也要有個還原的啦,今天我們就把那個還原的做一下

其實很簡單的,就是把我們上一次備份的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;}}};@Overrideprotected 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(){@Overridepublic 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);}}});}@Overridepublic 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(){@Overridepublic 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(){@Overridepublic 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();//建立一個LooperToast.makeText(getApplicationContext(), "還原成功", Toast.LENGTH_SHORT).show();Looper.loop();//輪循一次Looper}catch (Exception e){e.printStackTrace();Looper.prepare();//建立一個LooperToast.makeText(getApplicationContext(), "還原失敗", Toast.LENGTH_SHORT).show();Looper.loop();//輪循一次Looper}}}.start();}}

好啦,恢複簡訊的操作到這裡,也完成啦

今天源碼下載



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.