Monday, June 18, 2012

How To Display Notification In Android




The project describes how to display notifications in your application.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project NotificationExample
2.) Make sure to add following in your res/values/strings.xml file:
<resources>
    <string name="info">Notification Example, press the button Notification to add a notification.</string>

    <string name="app_name">NotificationExample</string>
        <string name="btn_default_notification_text">Show Notification</string>
        <string name="notification_string">This is the activity started when you press the notification.</string>
</resources>
3.) Insert following code in your main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
    <LinearLayout
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="0.9">
     
                <TextView  
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content" 
               android:gravity="center_vertical|center_horizontal"
               android:text="@string/info"/>
    </LinearLayout>
     
    <LinearLayout
           android:orientation="horizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="0.1"
           android:gravity="bottom|center_horizontal">
         
                    <Button
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_weight="1"
               android:id="@+id/btn_default_notification"
               android:text="@string/btn_default_notification_text"/>
             
    </LinearLayout>
</LinearLayout>
4.) Create and insert following code in res/layout/notification_viewer.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
        <LinearLayout
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="0.9">
     
                <TextView  
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content" 
               android:gravity="center_vertical|center_horizontal"
               android:text="@string/notification_string"/>
          </LinearLayout>
</LinearLayout>
5.) Create and insert following code in res/layout/customlayout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="horizontal">
        <ImageView
               android:id="@+id/custom_view_image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_weight="0.1"
               android:src="@drawable/icon"
               android:scaleType="fitCenter">
        </ImageView>
     
        <LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:orientation="vertical"
               android:gravity="center_vertical"
               android:layout_marginTop="5dp"
               android:layout_marginBottom="5dp"
               android:layout_weight="0.9">
                <TextView
                       android:id="@+id/custom_view_text"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content"
                       android:layout_weight="0.9"
                       android:gravity="center_vertical"
                       android:text="Sample message"
                       android:textColor="#000">
                </TextView>
             
                <ProgressBar
                       style="?android:attr/progressBarStyleHorizontal"
                       android:id="@+id/custom_view_progress"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content"
                       android:layout_weight="0.9"
                       android:max="100"
                       android:gravity="center_vertical">
                </ProgressBar>
        </LinearLayout>
</LinearLayout>
6.) Insert following in your manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.example.NotificationExample"
     android:versionCode="1"
     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".NotificationExample"
                 android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
       </activity>
     
        <activity android:name=".NotificationViewer"/>
    </application>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
</manifest>
7.) Create and insert following code in src/org.example.NotificationExample/NotificationViewer.java:
package org.example.NotificationExample;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
public class NotificationViewer extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.notification_viewer);
             
                NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
             
                notificationManager.cancel(Constants.NOTIFICATION_ID);
        }
}
8.) Create and insert following code in src/org.example.NotificationExample/Constants.java:
package org.example.NotificationExample;
public interface Constants
{
        public static final int NOTIFICATION_ID = 10001;
        static final int PROGRESS_MAX = 100;
}
9.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. NotificationExample.
2.) Enter following information:
Project name: NotificationExample
Build Target: Android 2.3.3
Application name: NotificationExample
Package name: org.example. NotificationExample
Create Activity: NotificationExample
On Clicking Finish NotificationExample code structure is generated with the necessary Android Packages being imported along with NotificationExample.java. NotificationExample class will look like following:
package org.example.NotificationExample;
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.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class NotificationExample extends Activity
{
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
       ((Button)findViewById(R.id.btn_default_notification)).setOnClickListener(btnClick);
    }
 
    private void addDefaultNotification(){
        NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
     
        int icon = R.drawable.icon;
        CharSequence text = "Notification Text";
        CharSequence contentTitle = "Notification Title";
        CharSequence contentText = "Sample notification text.";
        long when = System.currentTimeMillis();
     
        Intent intent = new Intent(this, NotificationViewer.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new Notification(icon,text,when);
     
        long[] vibrate = {0,100,200,300};
        notification.vibrate = vibrate;
     
        notification.ledARGB = Color.RED;
        notification.ledOffMS = 300;
        notification.ledOnMS = 300;
     
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        //notification.flags |= Notification.FLAG_SHOW_LIGHTS;
     
        notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
     
        notificationManager.notify(Constants.NOTIFICATION_ID, notification);
    }
 
    private final View.OnClickListener btnClick = new View.OnClickListener() {
             
                @Override
                public void onClick(View v) {
                        switch(v.getId())
                        {
                                case R.id.btn_default_notification:
                                {
                                        addDefaultNotification();
                                     
                                        break;
                                }
                        }
                }
        };
        }
Output – The final output:

No comments:

Post a Comment