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





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 = (Button) findViewById(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[] { 100, 250, 100, 500};
        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