开发者

Notification Error When Downloading

I'm using some code from the internet in this - but i still don't know why it's not working... If i take out the Notification things, then it downloads fine (in the background - no good!). But whenever i include them, it forces quit, and i'm not sure why!

Notification Setup

    Intent intent = new Intent(this, ListActivity.class);
    final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

    // configure the notification
    final Notification notification = new Notification(R.drawable.icon, "Downloading Rom via RomGet", System
            .currentTimeMillis());
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
    notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.layout_download);
    notification.contentIntent = pendingIntent;
    notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon_rom);
    notification.contentView.setTextViewText(R.id.download_description, "Downloading");
    notification.contentView.setProgressBar(R.id.download_progress, 100, 0, false);
    notification.icon = R.drawable.ic_stat_rom;

    final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
            getApplicationContext().NOTIFICATION_SERVICE);

    notificationManager.notify(43, notification);

Download Class

    public void run() {         
        try {
            // Setup download things here - all good

            totalSize = urlConnection.getContentLength();

            //create a buffer...
            byte[] buffer = new byte[1024];
            int bufferLength = 0; //used to store a temporary size of the buffer

            //now, read through the input buffer and write the contents to the file
            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                //add the data in the buffer to the file in the file output stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;
                //this is where you would do something to report the prgress, like this maybe

                handler.sendEmptyMessage(0);
            }
            //close the output stream when done
            fileOutput.close();

        //catch some possible errors...
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // see http://androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notificationØ
    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            //notification.setProgressBar(R.id.download_progress, Math.round(totalSize), Math.round(downloadedSize), false);
            notification.contentView.setTextViewText(R.id.download_description, Integer.toString(downloadedSize));
            // inform the progress bar of updates in progress
            notificationManager.notify(43, notification);
        }
    };
}

This is the error it is giving me:

05-31 22:48:23.539: DEBUG/AndroidRuntime(24431): Shutting down VM
05-31 22:48:23.539: WARN/dalvikvm(24431): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431): FATAL EXCEPTION: main
05-31 22:48:23.542: ERROR/AndroidRuntime(24431): java.lang.NullPointerException
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at com.ezeh.romget.ListActivity$DownloadThread$1.handleMessage(ListActivity.java:285)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at android.os.Looper.loop(Looper.java:123)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at android.app.ActivityThread.main(ActivityThread.java:3835)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at java.lang.reflect.Method.invoke(Method.java:507)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-31 22:48:23.542: ERROR/AndroidRuntime(24431):     at dalvik.system.NativeStart.main(Native Method)
05-31 22:48:23.558: WARN/ActivityManager(13390):   Force finishing activity com.ezeh.romget/.ListActivity
05-31 22:48:24.074: WARN/ActivityManager(13390): Activity pause timeout for HistoryRecord{407e8090 com.ezeh.romget/.ListActivity}
05-31 22:48:29.386: INFO/Process(24431): Sending signal. PID: 24431 SIG: 9
05-31 22:48:29.460: INFO/ActivityManager(13390): Process com.ezeh.romget (pid 24431) has died.
05-31 22:48:29.464: INFO/WindowManager(13390): WIN DEATH: Window{408fbb18 com.ezeh.romget/com.ezeh.romget.ListActivity paused=false}
05-31 22:48:29.468: INFO/DemoService(13809): DiyScheduer.onStart
05-31 22:48:29.468: INFO/ggheart(13809): onStart
05-31 22:48:29.472: INFO/WindowManager(13390): WIN DEATH: Window{40863a78 com开发者_如何学运维.ezeh.romget/com.ezeh.romget.HomeActivity paused=false}
05-31 22:48:29.484: INFO/rxq(13809): onResume()
05-31 22:48:34.644: DEBUG/dalvikvm(24165): GC_EXPLICIT freed 87K, 49% free 3507K/6791K, external 0K/0K, paused 28ms


To your new problem with the app freezing while downloading, you are actually blocking the UI process with the download itself. The way around that is to use an AsyncTask to do the downloading in the background. Google has an intro to AsyncTasks here. There are plenty of other good resources on how to download files in the background online.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜