jeudi 23 juillet 2015

How to auto restore SQLite Database of our android app at certain time of day?

I have a task of restoring SQLite Database of app from SD card automatically at certain time of day. I have restored the database of app using Button Click event. But I don't know how to restore database automatically without any click event. I want the task to be donned automatically at a specific time. I have provided my manual code here. Please help me.

  private void importDB() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        File myDir=new File(Environment.getExternalStorageDirectory(),"ST_Manager");
        myDir.mkdir();

        if (sd.canWrite()) {
            String currentDBPath = "//data//com.realtech.st_manager_v11//databases//unitdb";
            String backupDBPath = "//ST_Manager//unitdb.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

                FileChannel src = new FileInputStream(backupDB).getChannel();
                FileChannel dst = new FileOutputStream(currentDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
        }
    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Restoration Failed", Toast.LENGTH_SHORT).show();
    }
}

I have done this using onMenuItemClick Event. Please help me out in auto restoring. Thanks.

Aucun commentaire:

Enregistrer un commentaire