Cancel the notification that we started : Notification « Core Class « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » Core Class » Notification 




Cancel the notification that we started
  
package app.test;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

 class NotificationView extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.row);
            
        NotificationManager nm = (NotificationManager
            getSystemService(NOTIFICATION_SERVICE);
        nm.cancel(getIntent().getExtras().getInt("notificationID"));
    }
}
public class Test extends Activity {
  int notificationID = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (ButtonfindViewById(R.id.btn_displaynotif);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                displayNotification();     
            }
        });    
    }
    
    protected void displayNotification()
    {
      Intent i = new Intent(this, NotificationView.class);
      i.putExtra("notificationID", notificationID);    
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);        
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE)
        Notification notif = new Notification(
            R.drawable.icon, 
            "Reminder: Meeting starts in 5 minutes",
            System.currentTimeMillis());

        CharSequence from "System Alarm";
        CharSequence message = "Meeting with customer at 3pm...";
        
        notif.setLatestEventInfo(this, from, message, pendingIntent);

        notif.vibrate = new long[] { 100250100500};
        nm.notify(notificationID, notif);        
    }
}

//main.xml

<?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/btn_displaynotif"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Display Notification" />
</LinearLayout>
//row.xml
<?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" >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Here are the details for the notification..." />
</LinearLayout>

   
    
  














Related examples in the same category
1.Notification event
2.How to use Notification
3.Look up the notification manager service
4.Using NotificationManager
5.Demonstrates adding notifications to the status bar
6.Send Notification
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.