wah h.
- Forum-Beiträge: 35
11.03.2011, 19:18:26 via Website
11.03.2011 19:18:26 via Website
Guten Abend alle zusammen,
bin gerade dabei einen background service zu schreiben der eine mp3 Datei herrunterläd.
Außerdem möchte ich den Prozess in einem Progressbar in der Notificationsbar anzeigen lassen.
Ich hab es soweit hinbekommen, dass die datei im Hintergrund runtergeladen wird. Aber während des download Prozesses
wird das handy dann aufeinmal total langsam und reagiert sehr schlecht.
was mache ich falsch?
bin gerade dabei einen background service zu schreiben der eine mp3 Datei herrunterläd.
Außerdem möchte ich den Prozess in einem Progressbar in der Notificationsbar anzeigen lassen.
Ich hab es soweit hinbekommen, dass die datei im Hintergrund runtergeladen wird. Aber während des download Prozesses
wird das handy dann aufeinmal total langsam und reagiert sehr schlecht.
was mache ich falsch?
1public class DownloadService extends Service {
2 private static final String TAG = "MyService";
3 private String link;
4
5 private File file;
6 private DownloadThread downloadThread;
7 private int progress = 0;
8 private Notification notification;
9 private NotificationManager mNotificationManager;
10
11
12
13 @Override
14 public IBinder onBind(Intent intent) {
15 return null;
16 }
17
18 @Override
19 public void onCreate() {
20 CharSequence tickerText = "Hello";
21 long when = System.currentTimeMillis();
22 Context context = getApplicationContext();
23 mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(
24 context.NOTIFICATION_SERVICE);
25
26 notification = new Notification(android.R.drawable.ic_input_get, tickerText, when);
27 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_download);
28 contentView.setProgressBar(R.id.notification_download_progressBar, 100, progress, false);
29 contentView.setTextViewText(R.id.notification_download_text, "simulation in progress");
30 notification.contentView = contentView;
31
32 Intent notificationIntent = new Intent(this, DownloadService.class);
33 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
34 notification.contentIntent = contentIntent;
35 mNotificationManager.notify(65, notification);
36
37 }
38
39
40 @Override
41 public void onDestroy() {
42 Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
43 Log.d(TAG, "onDestroy");
44 }
45
46 @Override
47 public void onStart(Intent intent, int startid) {
48 link = intent.getExtras().getString("link");
49 downloadThread = new DownloadThread(link, Environment.getExternalStorageDirectory()+ "/test/test1.mp3");
50 downloadThread.start();
51 }
52
53 private class DownloadThread extends Thread {
54 private final String TAG = DownloadService.class.getSimpleName();
55 private static final int BUFFER_LENGTH = 8192;
56
57 private String fileurl;
58 private String filepath;
59
60 public DownloadThread(String fileurl, String filepath) {
61 this.fileurl = fileurl;
62 this.filepath = filepath;
63 }
64
65 public void run() {
66 try {
67 URL url = new URL(fileurl);
68 file = new File(filepath);
69
70
71 URLConnection ucon = url.openConnection();
72 int length = ucon.getContentLength();
73
74 /*
75 * Define InputStreams to read from the URLConnection and
76 * OutputStream to write to file
77 */
78 InputStream is = ucon.getInputStream();
79 BufferedInputStream bis = new BufferedInputStream(is);
80 FileOutputStream fos = new FileOutputStream(file);
81
82 /*
83 * Read bytes to the Buffer until there is nothing more to read(-1).
84 */
85 byte[] buffer = new byte[BUFFER_LENGTH];
86 int read;
87 int count = 0;
88
89 while ((read = bis.read(buffer, 0, BUFFER_LENGTH)) != -1) {
90 fos.write(buffer, 0, read);
91 count += read;
92 progress = count*100/length;
93
94 notification.contentView.setProgressBar(R.id.notification_download_progressBar, 100, count*100/length, false);
95 mNotificationManager.notify(65, notification);
96
97
98
99 }
100 fos.close();
101 } catch (IOException e) {
102 Log.d(TAG, "Error: " + e);
103 }
104
105 }
106 }
107}
2 private static final String TAG = "MyService";
3 private String link;
4
5 private File file;
6 private DownloadThread downloadThread;
7 private int progress = 0;
8 private Notification notification;
9 private NotificationManager mNotificationManager;
10
11
12
13 @Override
14 public IBinder onBind(Intent intent) {
15 return null;
16 }
17
18 @Override
19 public void onCreate() {
20 CharSequence tickerText = "Hello";
21 long when = System.currentTimeMillis();
22 Context context = getApplicationContext();
23 mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(
24 context.NOTIFICATION_SERVICE);
25
26 notification = new Notification(android.R.drawable.ic_input_get, tickerText, when);
27 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_download);
28 contentView.setProgressBar(R.id.notification_download_progressBar, 100, progress, false);
29 contentView.setTextViewText(R.id.notification_download_text, "simulation in progress");
30 notification.contentView = contentView;
31
32 Intent notificationIntent = new Intent(this, DownloadService.class);
33 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
34 notification.contentIntent = contentIntent;
35 mNotificationManager.notify(65, notification);
36
37 }
38
39
40 @Override
41 public void onDestroy() {
42 Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
43 Log.d(TAG, "onDestroy");
44 }
45
46 @Override
47 public void onStart(Intent intent, int startid) {
48 link = intent.getExtras().getString("link");
49 downloadThread = new DownloadThread(link, Environment.getExternalStorageDirectory()+ "/test/test1.mp3");
50 downloadThread.start();
51 }
52
53 private class DownloadThread extends Thread {
54 private final String TAG = DownloadService.class.getSimpleName();
55 private static final int BUFFER_LENGTH = 8192;
56
57 private String fileurl;
58 private String filepath;
59
60 public DownloadThread(String fileurl, String filepath) {
61 this.fileurl = fileurl;
62 this.filepath = filepath;
63 }
64
65 public void run() {
66 try {
67 URL url = new URL(fileurl);
68 file = new File(filepath);
69
70
71 URLConnection ucon = url.openConnection();
72 int length = ucon.getContentLength();
73
74 /*
75 * Define InputStreams to read from the URLConnection and
76 * OutputStream to write to file
77 */
78 InputStream is = ucon.getInputStream();
79 BufferedInputStream bis = new BufferedInputStream(is);
80 FileOutputStream fos = new FileOutputStream(file);
81
82 /*
83 * Read bytes to the Buffer until there is nothing more to read(-1).
84 */
85 byte[] buffer = new byte[BUFFER_LENGTH];
86 int read;
87 int count = 0;
88
89 while ((read = bis.read(buffer, 0, BUFFER_LENGTH)) != -1) {
90 fos.write(buffer, 0, read);
91 count += read;
92 progress = count*100/length;
93
94 notification.contentView.setProgressBar(R.id.notification_download_progressBar, 100, count*100/length, false);
95 mNotificationManager.notify(65, notification);
96
97
98
99 }
100 fos.close();
101 } catch (IOException e) {
102 Log.d(TAG, "Error: " + e);
103 }
104
105 }
106 }
107}
— geändert am 11.03.2011, 19:20:37