4

I have a problem that I can't explain. I have a main activity which contains a button. When this button is clicked, it launches a new activity which downloads an xml file from the internet. While downloading there is a notification that indicates the download progress.

This works perfectly in my AVD (API level 17). But, when I try to do the same thing in my device (GT-I1900 with API level 10, android version 2.3.3) it crashes.

I think that the problem exists with the notificationBuilder that I use.

Context context = getApplicationContext();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("Download Progress").setContentText("Downloading").setSmallIcon(android.R.drawable.stat_sys_download);

...

notificationManager.notify(0, notificationBuilder.build());

For the download I use a DownloadFilesTask (extends AsyncTask) class which I have tested and works fine.

Here is the exception log:

    at android.app.NotificationManager.notify(NotificationManager.java:91)

So my question is: if the problem do exists with the API level of my device, can anyone explain how to use notification builder for lower APIs.

Here is the full log:

02-01 23:06:28.862: E/AndroidRuntime(16860): FATAL EXCEPTION: AsyncTask #1
02-01 23:06:28.862: E/AndroidRuntime(16860): java.lang.RuntimeException: An error occured while executing doInBackground()
02-01 23:06:28.862: E/AndroidRuntime(16860):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at java.lang.Thread.run(Thread.java:1019)
02-01 23:06:28.862: E/AndroidRuntime(16860): Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=com.bakoproductions.easybet id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at android.os.Parcel.readException(Parcel.java:1326)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at android.os.Parcel.readException(Parcel.java:1276)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:322)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at android.app.NotificationManager.notify(NotificationManager.java:111)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at android.app.NotificationManager.notify(NotificationManager.java:91)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at com.bakoproductions.easybet.XMLParser.downloadXML(XMLParser.java:83)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at com.bakoproductions.easybet.DownloadFilesTask.doInBackground(DownloadFilesTask.java:24)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at com.bakoproductions.easybet.DownloadFilesTask.doInBackground(DownloadFilesTask.java:1)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-01 23:06:28.862: E/AndroidRuntime(16860):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-01 23:06:28.862: E/AndroidRuntime(16860):    ... 4 more
Mahm00d
  • 3,881
  • 8
  • 44
  • 83

1 Answers1

6

In older android versions, your Notification has to have a content Intent, so that when the user clicks your Notification, something happens. This means you must:

Make an Intent object, pointing somewhere, such as your MainActivity. Make sure to add the Intent.FLAG_ACTIVITY_NEW_TASK flag.

Intent intent = new Intent (this, MainActivity.class);
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

Convert it to a PendingIntent

PendingIntent pend = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Finally, in the builder, call

notificationBuilder.setContentIntent (pend);
A--C
  • 36,351
  • 10
  • 106
  • 92