2011-05-31 113 views
0

我在網上使用了一些代碼 - 但我仍然不知道爲什麼它不工作... 如果我拿出Notification的東西,那麼它下載罰款(在後臺- 不好!)。但是,每當我包括他們,它迫使退出,我不知道爲什麼!下載時出現通知錯誤

通知設置

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); 

下載類

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); 
     } 
    }; 
} 

這是它給我的錯誤:

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 
+0

如果您包含堆棧跟蹤,它可能會有所幫助。 – DKIT 2011-05-31 12:37:05

+0

我包含LogCat摘要。歡呼聲 – jsw 2011-05-31 12:54:57

+0

你有什麼在線ListActivity.java:285?看起來有些東西是空的。 – rochdev 2011-05-31 13:03:53

回答

2

要使用的應用程序凍結你的新問題而下載,你實際上是通過下載本身阻止UI進程。解決方法是使用AsyncTask在後臺執行下載。 Google有一個AsyncTasks here的介紹。還有很多關於如何在線下載後臺文件的好資源。

+0

我沒有使用異步任務,但我在另一個線程中運行下載沒有?我的意思是,如果我不發佈任何通知,那麼下載在後臺運行就好(我只是沒有可視化,我檢查文件大小,因爲它在文件瀏覽器中更新)如何工作? Asynctask更適合使用而不是另一個線程?歡呼 – jsw 2011-05-31 13:41:47

+0

Android中的線程並不真正工作(我發佈的第二個鏈接解釋了這個問題)。 AsyncTask會阻止應用程序凍結。 – Haphazard 2011-05-31 13:43:09

+0

聽起來很棒,正是我需要的,歡呼!另外,如果我正在下載一系列文件類型,那麼擴展該類的正確方法是什麼? (大部分是zip)?我發現擴展AsyncTask 具體是關於輸入一個字符串(好),但輸出一個位圖?還是有什麼我失蹤?乾杯 – jsw 2011-05-31 13:51:49

相關問題