- Forum-Beiträge: 1.148
30.07.2011, 11:07:36 via Website
30.07.2011 11:07:36 via Website
ich versuche derzeit, eine einzelne App aus einer ListView auszuwählen und diese zu sichern.
(Ich weiss nicht genau wie ich das Ausgewählte Element der ListView nutze)
Mein Ansatz dazu sieht folgendermassen aus:
2
3 backup_app = ProgressDialog.show(MainActivity.this,
4 "Sicherung", "", true, false);
5 Thread backup_app = new Thread(new BackupApp());
6 backup_app.start();
Aufgerufen wird:
2 .getExternalStorageDirectory() + "/InfoBackup/";
3 private static final int SET_PROGRESS = 0x00;
4 private static final int FINISH_PROGRESS = 0x01;
5 private ProgressDialog backup_app;
6
7 private List<App> mApps;
8 private App info;
9
10private class BackupApp implements Runnable {
11 private static final int BUFFER = 1024;
12
13 private File mDir = new File(BACKUP_LOC);
14 private byte[] mData;
15
16 public BackupApp() {
17 mData = new byte[BUFFER];
18
19 /* create dir if needed */
20 File d = new File(BACKUP_LOC);
21 if (!d.exists()) {
22 d.mkdir();
23
24 // then create this directory
25 mDir.mkdir();
26
27 } else {
28 if (!mDir.exists())
29 mDir.mkdir();
30 }
31 }
32
33 public void run() {
34 BufferedInputStream mBuffIn;
35 BufferedOutputStream mBuffOut;
36 Message msg;
37 int read = 0;
38 info = (App) mAppsList.getSelectedItem();
39 String source_file = info.getInstallDir() + info.getPackageName();
40 String out_file = info.getTitle() + " " + info.getVersionName()
41 + ".apk";
42
43 try {
44 mBuffIn = new BufferedInputStream(new FileInputStream(
45 source_file));
46 mBuffOut = new BufferedOutputStream(new FileOutputStream(
47 BACKUP_LOC + out_file));
48
49 while ((read = mBuffIn.read(mData, 0, BUFFER)) != -1)
50 mBuffOut.write(mData, 0, read);
51
52 mBuffOut.flush();
53 mBuffIn.close();
54 mBuffOut.close();
55
56 msg = new Message();
57 msg.what = SET_PROGRESS;
58 msg.obj = info.getTitle() + " wird gesichert";
59 BackupAppHandler.sendMessage(msg);
60
61 } catch (FileNotFoundException e) {
62 e.printStackTrace();
63 } catch (IOException e) {
64 e.printStackTrace();
65 }
66 BackupAppHandler.sendEmptyMessage(FINISH_PROGRESS);
67 }
68 }
69
70 private Handler BackupAppHandler = new Handler() {
71 public void handleMessage(Message msg) {
72
73 switch (msg.what) {
74 case SET_PROGRESS:
75 backup_app.setMessage((String) msg.obj);
76 break;
77 case FINISH_PROGRESS:
78 backup_app.dismiss();
79 AlertDialog.Builder finish = new AlertDialog.Builder(
80 MainActivity.this);
81 finish.setTitle(R.string.finish_title);
82 finish.setMessage(R.string.finish_message);
83 finish.setCancelable(false);
84 finish.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
85 finish.setPositiveButton(R.string.finish_button,
86 new DialogInterface.OnClickListener() {
87 public void onClick(DialogInterface dialog, int id) {
88 dialog.dismiss();
89 }
90 });
91 AlertDialog finish_alert = finish.create();
92 finish_alert.show();
93 break;
94 }
95 }
96 };
Ich habe meinen Code, mit dem ich alle Apps sichern kann als Vorlage genommen. (Hier damit ihr einen Vergleich habt)
2
3 backup_all = ProgressDialog.show(MainActivity.this,
4 "Sicherung der Anwendungen", "", true, false);
5 Thread backup_all = new Thread(new BackupAll(mApps));
6 backup_all.start();
Aufgerufen wird dort:
2 .getExternalStorageDirectory() + "/InfoBackup/";
3 private static final int SET_PROGRESS = 0x00;
4 private static final int FINISH_PROGRESS = 0x01;
5 private ProgressDialog backup_all;
6
7 private List<App> mApps;
8 private App info;
9
10private class BackupAll implements Runnable {
11 private static final int BUFFER = 1024;
12
13 private File mDir = new File(BACKUP_LOC);
14 private byte[] mData;
15
16 public BackupAll(List<App> data) {
17 mApps = data;
18 mData = new byte[BUFFER];
19
20 /* create dir if needed */
21 File d = new File(BACKUP_LOC);
22 if (!d.exists()) {
23 d.mkdir();
24
25 // then create this directory
26 mDir.mkdir();
27
28 } else {
29 if (!mDir.exists())
30 mDir.mkdir();
31 }
32 }
33
34 public void run() {
35 BufferedInputStream mBuffIn;
36 BufferedOutputStream mBuffOut;
37 Message msg;
38 int len = mApps.size();
39 int read = 0;
40
41 for (int i = 0; i < len; i++) {
42 info = mApps.get(i);
43 String source_dir = info.getInstallDir();
44 String out_file = info.getTitle() + " " + info.getVersionName()
45 + ".apk";
46 try {
47 mBuffIn = new BufferedInputStream(new FileInputStream(
48 source_dir));
49 mBuffOut = new BufferedOutputStream(new FileOutputStream(
50 BACKUP_LOC + out_file));
51
52 while ((read = mBuffIn.read(mData, 0, BUFFER)) != -1)
53 mBuffOut.write(mData, 0, read);
54
55 mBuffOut.flush();
56 mBuffIn.close();
57 mBuffOut.close();
58
59 msg = new Message();
60 msg.what = SET_PROGRESS;
61 msg.obj = i + " von " + len + " Anwendungen gesichert";
62 BackupAllHandler.sendMessage(msg);
63
64 } catch (FileNotFoundException e) {
65 e.printStackTrace();
66 } catch (IOException e) {
67 e.printStackTrace();
68 }
69 }
70
71 BackupAllHandler.sendEmptyMessage(FINISH_PROGRESS);
72 }
73 }
Gruß Alexander
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.