2

I am trying to use a while loop in order to update a notification every second. However on 2.3.3 & below, it crashes with these logcat errors:

08-14 07:30:17.394: E/AndroidRuntime(501): FATAL EXCEPTION: main
08-14 07:30:17.394: E/AndroidRuntime(501): 
android.app.RemoteServiceException: Bad notification for startForeground: 
java.lang.IllegalArgumentException: contentIntent required: 
pkg=com.package.name id=77 
notification=Notification(vibrate=null,sound=null,defaults=0x4,flags=0x40)

The problem is even when I check Build.VERSION, the code crashes with the same logcat errors:

if (isRunning) {
        n.defaults = Notification.DEFAULT_LIGHTS;
        while (!RecordTimerTask.isRunning && isRunning) {
            long now = System.currentTimeMillis() - startTime;
            n.setLatestEventInfo(getApplicationContext(), getResources()
                    .getString(R.string.notify_title),
                    getDurationBreakdown(now), null);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                startForeground(FOREGROUND_ID, n);
            else 
                notify.notify(ID, n);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

It seems like devices running 2.3.3 & below don't like the null notification intents. What I don't understand is why I get a logcat error about startForeground when it's never called?

Denizen
  • 335
  • 5
  • 17

1 Answers1

4

The problem is you call setLatestEventInfo with a null argument for the pendingIntent. This works on Android ICS but not on earlier versions...

Have a look on the documentation for the setLatestEventInfo.

Here is how your code should look (if you are pushing these notifications from a service):

        // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, LocalServiceActivities.Controller.class), 0);

    // Set the info for the views that show in the notification panel.
    notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                   text, contentIntent);

Code was quoted from Android official documentation.

Also see this for a similar issue on StackOverflow.

Community
  • 1
  • 1
Radu
  • 2,076
  • 2
  • 20
  • 40
  • actually, in case you don't wish to handle the clicking on the notification, you can use an empty intent , as such: builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0); – android developer Feb 04 '14 at 09:41