Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1025461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:55:54+00:00 2026-05-16T11:55:54+00:00

Suppose I am creating an Android application that’s like an SMS app. The requirements

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:55:54+00:00Added an answer on May 16, 2026 at 11:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a splash screen that will display while my Android application loads. I'd
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
I am creating an android application which has to execute web requests in the
Suppose I'm creating a Wordpress website with user login and a small e-commerce. When
Lets suppose I am creating an application for the iphone with a webView down
Why is it that for user defined types when creating an array of objects
Within my Rails application, I'd like to generate requests that behave identically to genuine
Suppose you are implementing a publication database and creating migrations to represent different publications.
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.