1 package com.example.android.basicnotifications; 2 3 import android.app.Activity; 4 import android.app.NotificationManager; 5 import android.app.PendingIntent; 6 import android.content.Intent; 7 import android.graphics.BitmapFactory; 8 import android.net.Uri; 9 import android.os.Bundle; 10 import android.support.v4.app.NotificationCompat; 11 import android.view.View; 12 13 /** 14 * The entry point to the BasicNotification sample. 15 */ 16 public class MainActivity extends Activity { 17 /** 18 * A numeric value that identifies the notification that we'll be sending. 19 * This value needs to be unique within this app, but it doesn't need to be 20 * unique system-wide. 21 */ 22 public static final int NOTIFICATION_ID = 1; 23 onCreate(Bundle savedInstanceState)24 public void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.sample_layout); 27 28 } 29 30 /** 31 * Send a sample notification using the NotificationCompat API. 32 */ sendNotification(View view)33 public void sendNotification(View view) { 34 35 // BEGIN_INCLUDE(build_action) 36 /** Create an intent that will be fired when the user clicks the notification. 37 * The intent needs to be packaged into a {@link android.app.PendingIntent} so that the 38 * notification service can fire it on our behalf. 39 */ 40 Intent intent = new Intent(Intent.ACTION_VIEW, 41 Uri.parse("http://developer.android.com/reference/android/app/Notification.html")); 42 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 43 // END_INCLUDE(build_action) 44 45 // BEGIN_INCLUDE (build_notification) 46 /** 47 * Use NotificationCompat.Builder to set up our notification. 48 */ 49 NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 50 51 /** Set the icon that will appear in the notification bar. This icon also appears 52 * in the lower right hand corner of the notification itself. 53 * 54 * Important note: although you can use any drawable as the small icon, Android 55 * design guidelines state that the icon should be simple and monochrome. Full-color 56 * bitmaps or busy images don't render well on smaller screens and can end up 57 * confusing the user. 58 */ 59 builder.setSmallIcon(R.drawable.ic_stat_notification); 60 61 // Set the intent that will fire when the user taps the notification. 62 builder.setContentIntent(pendingIntent); 63 64 // Set the notification to auto-cancel. This means that the notification will disappear 65 // after the user taps it, rather than remaining until it's explicitly dismissed. 66 builder.setAutoCancel(true); 67 68 /** 69 *Build the notification's appearance. 70 * Set the large icon, which appears on the left of the notification. In this 71 * sample we'll set the large icon to be the same as our app icon. The app icon is a 72 * reasonable default if you don't have anything more compelling to use as an icon. 73 */ 74 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); 75 76 /** 77 * Set the text of the notification. This sample sets the three most commononly used 78 * text areas: 79 * 1. The content title, which appears in large type at the top of the notification 80 * 2. The content text, which appears in smaller text below the title 81 * 3. The subtext, which appears under the text on newer devices. Devices running 82 * versions of Android prior to 4.2 will ignore this field, so don't use it for 83 * anything vital! 84 */ 85 builder.setContentTitle("BasicNotifications Sample"); 86 builder.setContentText("Time to learn about notifications!"); 87 builder.setSubText("Tap to view documentation about notifications."); 88 89 // END_INCLUDE (build_notification) 90 91 // BEGIN_INCLUDE(send_notification) 92 /** 93 * Send the notification. This will immediately display the notification icon in the 94 * notification bar. 95 */ 96 NotificationManager notificationManager = (NotificationManager) getSystemService( 97 NOTIFICATION_SERVICE); 98 notificationManager.notify(NOTIFICATION_ID, builder.build()); 99 // END_INCLUDE(send_notification) 100 } 101 } 102