我正在使用互联网上的一些代码 - 但我仍然不知道它为什么不起作用......
如果我拿出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