星空网 > 软件开发 > 操作系统

Android 手机卫士9

Android 手机卫士9images/loading.gif' data-original="http://images2015.cnblogs.com/blog/1044471/201610/1044471-20161019225015388-2010452573.png" >

 

AToolActivity.java

 1 protected void showSmsBackUpDialog() { 2   //1,创建一个带进度条的对话框 3   final ProgressDialog progressDialog = new ProgressDialog(this); 4   progressDialog.setIcon(R.drawable.ic_launcher); 5   progressDialog.setTitle("短信备份"); 6   //2,指定进度条的样式为水平 7   progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 8   //3,展示进度条 9   progressDialog.show();10   //4,直接调用备份短信方法即可11   new Thread(){12     @Override13     public void run() {14       String path = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"sms74.;15       SmsBackUp.backup(getApplicationContext(), path, new CallBack() {16         @Override17         public void setProgress(int index) {18           //由开发者自己决定,使用对话框还是进度条19           progressDialog.setProgress(index);20           pb_bar.setProgress(index);21         }22         23         @Override24         public void setMax(int max) {25           //由开发者自己决定,使用对话框还是进度条26           progressDialog.setMax(max);27           pb_bar.setMax(max);28         }29       });30       31       progressDialog.dismiss();32     }33   }.start();34 }

SmsBackUp.java

Android 手机卫士9Android 手机卫士9
 1 package com.itheima.mobilesafe74.engine; 2  3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import org. 7 import android.content.Context; 8 import android.database.Cursor; 9 import android.net.Uri; 10 import android.util. 11  12 public class SmsBackUp { 13   private static int index = 0; 14    15   //备份短信方法 16   //ProgressDialog pd  原来的对话框 17   //现在改为进度条 18    19   //A,传递一个进度条所在的对话框 20   //B,传递一个进度条 21    22   public static void backup(Context ctx,String path,CallBack callBack) { 23     FileOutputStream fos = null; 24     Cursor cursor = null; 25     try { 26       //需要用到的对象上下文环境,备份文件夹路径,进度条所在的对话框对象用于备份过程中进度的更新 27       //1,获取备份短信写入的文件 28       File file = new File(path); 29       //2,获取内容解析器,获取短信数据库中数据 30       cursor = ctx.getContentResolver().query(Uri.parse("content://sms/"),  31           new String[]{"address","date","type","body"}, null, null, null); 32       //3,文件相应的输出流 33       fos = new FileOutputStream(file); 34       //4,序列化数据库中读取的数据,放置到 35         36       //5,给次 37       newSerializer.setOutput(fos, "utf-8"); 38       //DTD( 39       newSerializer.startDocument("utf-8", true); 40        41       newSerializer.startTag(null, "smss"); 42        43       //6,备份短信总数指定 44       //A 如果传递进来的是对话框,指定对话框进度条的总数 45       //B  如果传递进来的是进度条,指定进度条的总数 46 //      pd.setMax(cursor.getCount()); 47        48       if(callBack!=null){ 49         callBack.setMax(cursor.getCount()); 50       } 51        52       //7,读取数据库中的每一行的数据写入到 53       while(cursor.moveToNext()){ 54         newSerializer.startTag(null, "sms"); 55          56         newSerializer.startTag(null, "address"); 57         newSerializer.text(cursor.getString(0)); 58         newSerializer.endTag(null, "address"); 59          60         newSerializer.startTag(null, "date"); 61         newSerializer.text(cursor.getString(1)); 62         newSerializer.endTag(null, "date"); 63          64         newSerializer.startTag(null, "type"); 65         newSerializer.text(cursor.getString(2)); 66         newSerializer.endTag(null, "type"); 67          68         newSerializer.startTag(null, "body"); 69         newSerializer.text(cursor.getString(3)); 70         newSerializer.endTag(null, "body"); 71          72         newSerializer.endTag(null, "sms"); 73          74         //8,每循环一次就需要去让进度条叠加 75         index++; 76         Thread.sleep(500); 77         //ProgressDialog可以在子线程中更新相应的进度条的改变 78          79         //A 如果传递进来的是对话框,指定对话框进度条的当前百分比 80         //B  如果传递进来的是进度条,指定进度条的当前百分比 81 //        pd.setProgress(index); 82          83         if(callBack!=null){ 84           callBack.setProgress(index); 85         } 86       } 87       newSerializer.endTag(null, "smss"); 88       newSerializer.endDocument(); 89     } catch (Exception e) { 90       e.printStackTrace(); 91     }finally{ 92       try { 93         if(cursor!=null && fos!=null){ 94           cursor.close(); 95           fos.close(); 96         } 97       } catch (IOException e) { 98         e.printStackTrace(); 99       }100     }101   }102   //回调103   //1.定义一个接口104   //2,定义接口中未实现的业务逻辑方法(短信总数设置,备份过程中短信百分比更新)105   //3.传递一个实现了此接口的类的对象(至备份短信的工具类中),接口的实现类,一定实现了上诉两个为实现方法(就决定了使用对话框,还是进度条)106   //4.获取传递进来的对象,在合适的地方(设置总数,设置百分比的地方)做方法的调用107   public interface CallBack{108     //短信总数设置为实现方法(由自己决定是用  对话框.setMax(max) 还是用  进度条.setMax(max))109     public void setMax(int max);110     //备份过程中短信百分比更新(由自己决定是用  对话框.setProgress(max) 还是用  进度条.setProgress(max))111     public void setProgress(int index);112   }113 }

SmsBackUp

 




原标题:Android 手机卫士9

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流