Monday, 25 March 2013

Notifications Demo

Demo:






1)Activity:


package com.venky.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.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NotificationDemoActivity extends Activity {
      /** Called when the activity is first created. */

      Button btnStartNotification, btnStopNotification;

      NotificationManager notificationManager;

      Notification notification;

      int NOTIFICATION_ID = 1;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnStartNotification = (Button) findViewById(R.id.button1);

            btnStopNotification = (Button) findViewById(R.id.button2);

            notificationManager = (NotificationManager)                                                                                                          getSystemService(Context.NOTIFICATION_SERVICE);

            int icon = R.drawable.ic_launcher;

            String tickerText = "venky you got a Notification";

            long when = System.currentTimeMillis();

            notification = new Notification(icon, tickerText, when);

            Intent intent = new Intent(getApplicationContext(),
                        NotificationDemoActivity.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(
                        getApplicationContext(), 0, intent, 0);

            notification
                        .setLatestEventInfo(getApplicationContext(),
                                    "Notification Title", "Notification Descreption",
                                    pendingIntent);

            btnStartNotification.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        notificationManager.notify(NOTIFICATION_ID, notification);
                  }
            });

            btnStopNotification.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        notificationManager.cancel(NOTIFICATION_ID);
                  }
            });
      }
}



 2)main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="80dp"
        android:text="Start Notification"
        android:textSize="20dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Stop Notification"
        android:textSize="20dp" />

</LinearLayout>



4)Downoad this Project Click Here

No comments:

Post a Comment