Notifications


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/notifyButton"
        android:text="@string/notify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
    ></Button>
    <Button
        android:id="@+id/cancelButton"
        android:text="@string/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"      
    ></Button>
</LinearLayout>
 


SimpleNotification.java


package moor.android.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.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SimpleNotification extends Activity {
   
    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());
      

        Button start = (Button)findViewById(R.id.notifyButton);
        Button cancel = (Button)findViewById(R.id.cancelButton);
       
        start.setOnClickListener(new OnClickListener() {
          
            public void onClick(View v) {
              
              
                Context context = getApplicationContext();
                CharSequence contentTitle = "Notification Details...";
                CharSequence contentText = "Browse Android Official Site by clicking me";
                Intent notifyIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.android.com"));
                PendingIntent intent =
                    PendingIntent.getActivity(SimpleNotification.this, 0,
                    notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
              
                notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
                mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
            }
        });
       
        cancel.setOnClickListener(new OnClickListener() {
          
            public void onClick(View v) {
              
                mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
            }
        });
    }
}


Screen shot :


 


No comments:

Post a Comment