当前位置:操作系统 > 安卓/Android >>

某android平板项目开发笔记---计划任务备份

前言:
很久,都没更新过这个系列了…因为,除了图表以外,然后就是数据库了,调试了一个多星期的Ormlite数据库,在最新版本中(orm 4.3.3)发现了几个比较严重的bug(例如,查找id的时候无法使用Long类型),不过,还好,ormlite社区还算活跃,bug,已经在预览中修复了.关于Ormlite数据库的话,园子里面已经有了写得很不错的教程了,我就不重复他们的劳动了.然后,数据库搞定了,就是写业务了,有这么一个业务,就是,要求,在某个时间点,对插入的数据进行后台更新,然后,就涉及到了使用计划任务这么一块知识,觉得有必要做下笔记,这块,以后应该也能用到
业务说明:
   在某时某刻进行数据库的备份.
相关知识:
1,时间操作
(1) 熟悉使用Calendar
1,作为定时任务,我们需要一个具体的定时时间,以前,我是用Date 类取毫秒数,然后进行计数的操作
1.1例如
//以下为以前的某项目算相隔天数的演示代码   
String startDate = mDateStart.getText().toString();
 
        String endDate = year + "-" + (month + 1) + "-" + day;
        Log.d("soap", startDate + "---" + endDate);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // long day=(startC.getTime()-endC.getTime())/(24*60*60*1000);
        try {
 
            Date start = df.parse(startDate);
            Date end = df.parse(endDate);
            Long d = (end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000);
            Log.d("soap", "相隔天数" + d);
            if (d > 60) {
 
                return false;
            } else {
                return true;
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
以前这样写,感觉挺傻的,希望大家不要学习了,这次的业务需求,如果,是以前的话,我会一个创建具体时间字符串,然后用SimpleDateFormat,获取毫秒数,这样个人感觉,很不直观,也麻烦,也不方便国际化,我们用Calendar做就非常简单了.
1.2 定时23:00 执行备份操作
竟然是定时任务,我们就要熟悉一个android的一个用于做定时任务的类
AlarmManager
各位,先去看一下官方文档,在接着看下去吧…
这个类是的初始化是必须要用Context.getSystemService(Context.ALARM_SERVICE).
以下为定时代码块
//初始化定时类
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
//设置定时时间,1分钟后执行
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 1);
//设置时间到了执行的services
Intent intent = new Intent();
//创建一个servcies
intent.setClass(this, UpdateStatics.class);
PendingIntent pi = PendingIntent.getService(this, 0, intent, 0);
//设置定时
//需要android.permission.SET_TIME 权限
//第一个参数为定时类型(一共有四种,具体参见官方文档),第二个参数为设置的定时时间为long类型,第三个为执行的目标
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
以上代码就会在,1分钟后,系统执行UpdateStatics 类.
如果,我们要设置具体的时间,只要具体设置Calendar的对象即可,例如23:00分执行
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
 
总结:
使用Calendar类操作时间,和进行时间计算,设置,是十分方便的事情,
2,备份操作
1, 建一个接口类用于,监听操作.
 public interface CompletionListener {
    void onBackupComplete();
    void onRestoreComplete();
    void onError(int errorCode);
}
2,创建一个异步备份的类
public class BackupTask extends AsyncTask<String, Void, Integer> implements CompletionListener{
     
    //定义常量
    public static final int BACKUP_SUCCESS =1;
    public static final int RESTORE_SUCCESS = 2;
    public static final int BACKUP_ERROR = 3;
    public static final int RESTORE_NOFLEERROR = 4;
    public static final String COMMAND_BACKUP = "backupDatabase";
    public static final String COMMAND_RESTORE = "restroeDatabase";
    private Context mContext;
     
    public BackupTask(Context context){
        this.mContext = context;
    }
     
    @Override
    protected Integer doInBackground(String... params) {
        //1,获得数据库路径
        File dbFile = mContext.getDatabasePath("xxx.db");
        //2,创建保存的数据库的路径
        File exportDir = new File(Environment.getExternalStorageDirectory(),"shopBackup");
        if(!exportDir.exists()){
            exportDir.mkdirs();
        }
        File backup = new File(exportDir, dbFile.getName());
        //3,检查操作
        String command = params[0];
        if(command.equals(COMMAND_BACKUP)){
            //复制文件
            try {
                backup.createNewFile();
                fileCopy(dbFile, backup);
                return BACKUP_SUCCESS;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return BACKUP_ERROR;
            }
        }else{
            return BACKUP_ERROR;
        }
         
         
    }
 
 
    private void fileCopy(File source, File dest) throws IOE

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,