views:

198

answers:

1

Suppose I am creating an Android application that's like an SMS app. The requirements are as follows:

  1. The user can receive multiple notifications, each one having a dynamic ID of type int.
  2. When a notification is selected, it loads an activity which displays a corresponding message (the SMS).
  3. The single notification that was selected should be automatically dismissed.

My idea for how to handle this was to use putExtra to add the integer ID to the intent, which would then be accessible from the intent within the activity it loads, which would then dismiss the notification that called it.

For my test case, here are the specs:

  1. Notifications will eventually be generated from a service, for now they are being spawned when the test user presses a button.
  2. When a notification is selected, the called activity toasts the message, then attempts to dismiss the notification. (For the sake of visibility)

Here are my problems:

  1. When the first notification is selected, it is correct. The notification is dismissed.
  2. When each successive notification is selected, the first notification's ID is shown, and nothing is dismissed.
  3. I am a Java novice, more accustomed to scripting languages (such as Perl, PHP, etc) :)

Here is my source:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:orientation = "vertical"
    android:layout_width = "fill_parent"
    android:layout_height = "fill_parent"
>
    <Button
        android:id="@+id/create_notification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text = "Create new notification"
    />

package org.test.notifydemo;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Random;

public class aRunNotificationDemo extends Activity
{
    private NotificationManager mNotificationManager;

    @Override
    public void onCreate( Bundle icicle )
    {
        super.onCreate( icicle );
        setContentView( R.layout.run_notify_demo );

        mNotificationManager = (NotificationManager) getSystemService( aRunNotificationDemo.NOTIFICATION_SERVICE );

        int close_notify_id = getIntent().getIntExtra( "notification_id", 0 );
        if ( close_notify_id != 0 )
        {
            Toast.makeText( aRunNotificationDemo.this, "Dimissing this notification: " + Integer.toString(close_notify_id), Toast.LENGTH_SHORT ).show();
            mNotificationManager.cancel( close_notify_id );
        }

        findViewById( R.id.create_notification ).setOnClickListener( new MyButtonListener() );
    }

    private class MyButtonListener implements Button.OnClickListener
    {
        public void onClick( View my_view )
        {
            Random randGen = new Random();
            int notify_id = randGen.nextInt();

            int icon = R.drawable.icon_notification_01;
            CharSequence tickerText = Integer.toString(notify_id) + " New SMS!";
            long when = System.currentTimeMillis();

            Notification my_notification = new Notification(icon, tickerText, when);

            Context context = getApplicationContext();
            CharSequence contentTitle = Integer.toString(notify_id) + " New SMS Available!";
            CharSequence contentText = Integer.toString(notify_id) + " There is a new SMS available.";
            Intent notificationIntent = new Intent( aRunNotificationDemo.this, aRunNotificationDemo.class );

            notificationIntent.putExtra( "notification_id", notify_id );

            PendingIntent contentIntent = PendingIntent.getActivity( aRunNotificationDemo.this, 0, notificationIntent, 0 );

            my_notification.setLatestEventInfo( context, contentTitle, contentText, contentIntent );

            mNotificationManager.notify( notify_id, my_notification );
        }
    }

}
+3  A: 

When activity is once created its onCreate() method is called. Next time it is displayed the method is not necessarily called. Try moving code which removes the notification to onResume() method. Get familiar with Activity life cycle.

And by the way it is easier than you think:

http://developer.android.com/reference/android/app/Notification.html#FLAG_AUTO_CANCEL

my_notification.flags |= Notification.FLAG_AUTO_CANCEL;

Put the code above when creating a Notification.

radek-k
Thanks, this is exactly the answer for which I was looking. It's hard for a Java-novice to find functional documentation for features such as this without reading the entire Android Developers website... we have no idea what to search for.I am familiar with the life cycle as a process, but I assumed that each new intent would re-instantiate the Activity. You know what they say about "assuming" :) Thanks again.
Slobaum