Custom Notifications in Notification Bar in Android
Following Example will illustrate on Creating and sending Custom Notifications in Notification bar and clearing the created notifications too.
The Documents regarding the Notification Manager and Notification bar can be found here and here.
I'm using this concepts and here is the working example for the same.
Following is the basic screen shot for the application.
On Pressing the "Trigger Notification" button an Notification will be seen in the Notification bar saying "One Notification Received!! " as follows:
Now, expand the Notification window by dragging, you will see the notification as follows,
Start with creating a new Android Project in Eclipse, and following is the layout and Activity code for the application.
Layout Code i.e. 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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notification Sample, Click On Button To See A Sample Notification Above!!"/>
<Button android:text="Trigger Notification"
android:id="@+id/btnNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="Clear Notification"
android:id="@+id/btnClearNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
Following is the Activity Code for the Application (NotificationExample.java):
In the above example, on clicking the button, Clear Notification, will clear the notification from the Notification bar.
I hope my example was useful.
Happy Coding :-))
The Documents regarding the Notification Manager and Notification bar can be found here and here.
I'm using this concepts and here is the working example for the same.
Following is the basic screen shot for the application.
On Pressing the "Trigger Notification" button an Notification will be seen in the Notification bar saying "One Notification Received!! " as follows:
Now, expand the Notification window by dragging, you will see the notification as follows,
Start with creating a new Android Project in Eclipse, and following is the layout and Activity code for the application.
Layout Code i.e. 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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notification Sample, Click On Button To See A Sample Notification Above!!"/>
<Button android:text="Trigger Notification"
android:id="@+id/btnNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="Clear Notification"
android:id="@+id/btnClearNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
Following is the Activity Code for the Application (NotificationExample.java):
package com.mayuri.notification.example;
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.view.View.OnClickListener;
public class NotificationExample extends Activity {
Button btnNotification,btnClearNotification;
int NOTFICATION_ID = 198990;
NotificationManager nm = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnNotification = (Button) this.findViewById(R.id.btnNotification);
btnNotification.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nm = (NotificationManager) getSystemService(NotificationExample.this.NOTIFICATION_SERVICE);
String MyText = "One Notification Received!!";
String TitleText = "Message From Mayuri";
String NotiText = "Hope you liked My Example :-)";
Notification mNotification = new
Notification(R.drawable.icon,MyText,System.currentTimeMillis());
Intent MyIntent = new Intent( getApplicationContext(), NotificationExample.class);
MyIntent.putExtra("extendedTitle", TitleText);
MyIntent.putExtra("extendedText" , NotiText);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent,0); mNotification.setLatestEventInfo(getApplicationContext(),TitleText,NotiText, StartIntent);
nm.notify(NOTFICATION_ID , mNotification );
}
});
btnClearNotification = (Button) this.findViewById(R.id.btnClearNotification);
btnClearNotification.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (nm!=null){
nm.cancel(NOTFICATION_ID);
}
}
});
}
}
I hope my example was useful.
Happy Coding :-))
Thanks
akm
www.cdacians.com
No comments:
Post a Comment