Einzelne App sichern

  • Antworten:10
  • Bentwortet
Alexander R.
  • Forum-Beiträge: 1.148

30.07.2011, 11:07:36 via Website

Hallo,
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:

1private ProgressDialog backup_app;
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:
1private static final String BACKUP_LOC = Environment
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)

1private ProgressDialog backup_all;
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:

1private static final String BACKUP_LOC = Environment
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

Antworten
Ansgar M
  • Forum-Beiträge: 1.544

30.07.2011, 11:23:38 via App

Hey,
Stichwort: onItemClickListener?
Oder wo genau hapert es?
Lg Ansgar

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

30.07.2011, 11:47:01 via App

Das onItem klappt...
Ich weiss nur nicht, wie ich die ausgewählte app aus der Liste an den "BackupApp" übergeben oder so...

Bei BackupAll werden alle aus der Liste gesichert klappt wunderbar..
Nun solls in BackupApp nur die ausgewählte sichern

Gruß Alexander

Antworten
Ansgar M
  • Forum-Beiträge: 1.544

30.07.2011, 12:28:00 via App

Was für einen Adapter verwendest du?
Lg Ansgar

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

30.07.2011, 12:35:05 via Website

Hi Ansgar,
schick mir mal ne Mail mit deiner GTalk Adresse...
Ist besser

AppListAdapter:
1package de.rocky.info;
2
3import java.util.List;
4import java.util.Map;
5
6import android.content.Context;
7import android.graphics.drawable.Drawable;
8import android.view.LayoutInflater;
9import android.view.View;
10import android.view.ViewGroup;
11import android.widget.BaseAdapter;
12import android.widget.ImageView;
13import android.widget.TextView;
14
15public class AppListAdapter extends BaseAdapter {
16
17 private LayoutInflater mInflater;
18
19 private List<App> mApps;
20 /** a map which maps the package name of an app to its icon drawable */
21 private Map<String, Drawable> mIcons;
22 private Drawable mStdImg;
23
24 /**
25 * Constructor.
26 *
27 * @param context the application context which is needed for the layout inflater
28 */
29 public AppListAdapter(Context context) {
30 // cache the LayoutInflater to avoid asking for a new one each time
31 mInflater = LayoutInflater.from(context);
32
33 // set the default icon until the actual icon is loaded for an app
34 mStdImg = context.getResources().getDrawable(R.drawable.icon);
35 }
36
37 @Override
38 public int getCount() {
39 return mApps.size();
40 }
41
42 @Override
43 public Object getItem(int position) {
44 return mApps.get(position);
45 }
46
47 @Override
48 public long getItemId(int position) {
49 return position;
50 }
51
52 @Override
53 public View getView(int position, View convertView, ViewGroup parent) {
54
55 AppViewHolder holder;
56 if(convertView == null) {
57 convertView = mInflater.inflate(R.layout.list_item_app, null);
58
59 // creates a ViewHolder and stores a reference to the children view we want to bind data to
60 holder = new AppViewHolder();
61 holder.mTitle = (TextView) convertView.findViewById(R.id.app_name);
62 holder.mVersion = (TextView) convertView.findViewById(R.id.app_version);
63 holder.mSize = (TextView) convertView.findViewById(R.id.app_size);
64 holder.mDate = (TextView) convertView.findViewById(R.id.app_date);
65 holder.mIcon = (ImageView) convertView.findViewById(R.id.app_icon);
66 convertView.setTag(holder);
67 } else {
68 // reuse/overwrite the view passed assuming(!) that it is castable!
69 holder = (AppViewHolder) convertView.getTag();
70 }
71
72 App app = mApps.get(position);
73
74 holder.setTitle(app.getTitle());
75 if (mIcons == null || mIcons.get(app.getPackageName()) == null) {
76 holder.setIcon(mStdImg);
77 } else {
78 holder.setIcon(mIcons.get(app.getPackageName()));
79 }
80 holder.setVersion(app.getVersionName());
81 holder.setSize(app.getInstallSize());
82
83 return convertView;
84 }
85
86 /**
87 * Sets the list of apps to be displayed.
88 *
89 * @param list the list of apps to be displayed
90 */
91 public void setListItems(List<App> list) {
92 mApps = list;
93 }
94
95 /**
96 * Sets the map containing the icons for each displayed app.
97 *
98 * @param icons the map which maps the app's package name to its icon
99 */
100 public void setIcons(Map<String, Drawable> icons) {
101 this.mIcons = icons;
102 }
103
104 /**
105 * Returns the map containing the icons for each displayed app.
106 *
107 * @return a map which contains a mapping of package names to icon drawable for all displayed apps
108 */
109 public Map<String, Drawable> getIcons() {
110 return mIcons;
111 }
112
113 /**
114 * A view holder which is used to re/use views inside a list.
115 */
116 public class AppViewHolder {
117
118 private TextView mTitle;
119 private TextView mVersion;
120 private TextView mSize;
121 private TextView mDate;
122 private ImageView mIcon;
123
124 /**
125 * Sets the text to be shown as the app's title
126 *
127 * @param title the text to be shown inside the list row
128 */
129 public void setTitle(String title) {
130 mTitle.setText(title);
131 }
132
133 public void setVersion(String versionname) {
134 mVersion.setText(versionname);
135 }
136
137 public void setSize(String size) {
138 mSize.setText(size);
139 }
140
141 public void setDate(String date) {
142 mDate.setText(date);
143 }
144
145 /**
146 * Sets the icon to be shown next to the app's title
147 *
148 * @param img the icon drawable to be displayed
149 */
150 public void setIcon(Drawable img) {
151 if (img != null) {
152 mIcon.setImageDrawable(img);
153 }
154 }
155 }
156}


MainActivity
1private ListView mAppsList;
2 private AppListAdapter mAdapter;
3
4@Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.applist);
8
9 mAppsList = (ListView) findViewById(R.id.app_list_view);
10 mAppsList.setOnItemClickListener(this);
11
12 mApps = loadInstalledApps(INCLUDE_SYSTEM_APPS);
13
14 mAdapter = new AppListAdapter(getApplicationContext());
15 mAdapter.setListItems(mApps);
16 mAppsList.setAdapter(mAdapter);
17 }

— geändert am 30.07.2011, 13:04:28

Gruß Alexander

Antworten
Ansgar M
  • Forum-Beiträge: 1.544

30.07.2011, 21:34:53 via App

Danke für den Code.
Bin grad unterwegs (ab morgen kein Internet mehr, weil außerhalb Deutschland) und konnte dir deswegen keine PN schreiben.
So wie es aussieht, kannst du dir einfach die Position (der int Wert, glaub ich) aus dem onItemClick geben lassen und damit aus einem Adapter ein Item holen (sind bei dir ja Objekte der Klasse "App"). Oder hab ich da was übersehen?
Lg Ansgar

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

31.07.2011, 11:27:46 via Website

Danke fürdie Idee Ansgar...
Bin mir da jetzt nicht ganz sicher, wie genau ich das machen muss hätte da jemand ne idee

Gruß Alexander

Antworten
Florian B.
  • Forum-Beiträge: 284

31.07.2011, 15:00:47 via Website

So könnte dein OnListItemClickListener aussehen. Die Frage ist nur noch ob du das App Object an deine BackupApp Activity übergibst oder ob du nur die Id der App übergibst und sich die BackupApp Activity das App Objekt selber holt. Generell sollte man keine großen Objekte via Intents zwischen Activitys hin und her schieben.

1protected void onListItemClick(ListView listView, View view, int position, long id) {
2 AppListAdapter adapter = (AppListAdapter )listView.getListAdapter();
3
4 // Um das komplette Item zu bekommen
5 App clickedApp = (App)adapter.getItem(position);
6
7 // Um die Id des App objects zu bekommen
8 long clickedAppId = adapter.getItemId(position);
9
10 // App oder Id der App an die nächste Activity übergeben
11 ...
12}

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

31.07.2011, 15:12:35 via Website

Hallo Florian,
es geht ohne onClickListener (das item das ausgwählt wird ist schon in einem)....
Das was dickgedruckt ist muss als eine App aus der ListView genommen werden

1private class BackupApp implements Runnable {
2 private static final int BUFFER = 1024;
3
4 private File mDir = new File(BACKUP_LOC);
5 private byte[] mData;
6
7 public BackupApp() {
8 mData = new byte[BUFFER];
9
10 /* create dir if needed */
11 File d = new File(BACKUP_LOC);
12 if (!d.exists()) {
13 d.mkdir();
14
15 // then create this directory
16 mDir.mkdir();
17
18 } else {
19 if (!mDir.exists())
20 mDir.mkdir();
21 }
22 }
23
24 public void run() {
25 BufferedInputStream mBuffIn;
26 BufferedOutputStream mBuffOut;
27 Message msg;
28 int read = 0;
29 info = (App) mAppsList.getSelectedItem();
30 String source_file = info.getInstallDir() + info.getPackageName();
31 String out_file = info.getTitle() + " " + info.getVersionName()
32 + ".apk";
33
34 try {
35 mBuffIn = new BufferedInputStream(new FileInputStream(
36 source_file));
37 mBuffOut = new BufferedOutputStream(new FileOutputStream(
38 BACKUP_LOC + out_file));
39
40 while ((read = mBuffIn.read(mData, 0, BUFFER)) != -1)
41 mBuffOut.write(mData, 0, read);
42
43 mBuffOut.flush();
44 mBuffIn.close();
45 mBuffOut.close();
46
47 msg = new Message();
48 msg.what = SET_PROGRESS;
49 msg.obj = info.getTitle() + getString(R.string.apps_saveing);
50 BackupAppHandler.sendMessage(msg);
51
52 } catch (FileNotFoundException e) {
53 e.printStackTrace();
54 } catch (IOException e) {
55 e.printStackTrace();
56 }
57 BackupAppHandler.sendEmptyMessage(FINISH_PROGRESS);
58 }
59 }

— geändert am 31.07.2011, 15:13:40

Gruß Alexander

Antworten
San Blarnoi
  • Forum-Beiträge: 2.545

31.07.2011, 17:00:29 via Website

Willst du jetzt den irgendwo her kopierten Code so oft hier posten, bis dir jemand einen Codeschnipsel liefert, den du wiederum einfach kopieren kannst, ohne auch nur im geringsten zu verstehen, was da eigentlich geschieht?

Würdest du den Code nicht immer einfach nur kopieren, sondern auch versuchen ihn zu verstehen, wäre dein Problem längst gelöst, denn Ansgars und Florians Tipps waren natürlich völlig richtig.

Antworten
Alexander R.
  • Forum-Beiträge: 1.148

31.07.2011, 17:16:31 via Website

Ich müsste das Item an die Methode "BackupApp" übergeben...
Nur wie

(Leider bin ich noch nicht ganz so gut in Android/Java.... Ich lese ein Java Buch und mache die Übungen.... Aber durch learning by doing ist es doch interessanter)

— geändert am 31.07.2011, 17:16:41

Gruß Alexander

Antworten