Here below example show notification and cancel notification.
First i created application with 2 buttons one is for show and other one is for cancel.
when user click one show button notification will be appear and when user click on
cancel button cancel the notification.
Android UI layout(main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Notification" />
<Button
android:id="@+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel Notification" />
</LinearLayout>
Android Activity(MainActivity.java)
package com.androidsurya.notification;
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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
NotificationManager notificationManager;
Notification notificationObj;
PendingIntent pendingIntent;
String notificationTitle = "Notification Titile!";
String notificationText = "Notification text display";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button show = (Button) findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationObj = new Notification(R.drawable.ic_launcher,
"Show Notification !", System.currentTimeMillis());
pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
intent, Intent.FLAG_ACTIVITY_NEW_TASK);
notificationObj.defaults |= Notification.DEFAULT_SOUND;
notificationObj.setLatestEventInfo(getApplicationContext(),
notificationTitle, notificationText, pendingIntent);
notificationManager.notify(0, notificationObj);
}
});
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
notificationManager.cancel(0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
First i created application with 2 buttons one is for show and other one is for cancel.
when user click one show button notification will be appear and when user click on
cancel button cancel the notification.
Android UI layout(main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Notification" />
<Button
android:id="@+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel Notification" />
</LinearLayout>
Android Activity(MainActivity.java)
package com.androidsurya.notification;
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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
NotificationManager notificationManager;
Notification notificationObj;
PendingIntent pendingIntent;
String notificationTitle = "Notification Titile!";
String notificationText = "Notification text display";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button show = (Button) findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationObj = new Notification(R.drawable.ic_launcher,
"Show Notification !", System.currentTimeMillis());
pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
intent, Intent.FLAG_ACTIVITY_NEW_TASK);
notificationObj.defaults |= Notification.DEFAULT_SOUND;
notificationObj.setLatestEventInfo(getApplicationContext(),
notificationTitle, notificationText, pendingIntent);
notificationManager.notify(0, notificationObj);
}
});
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
notificationManager.cancel(0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Register Activity in Android Manifest file
<activity
android:name="com.androidsurya.notification.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Output Screenshot:
No comments:
Post a Comment