Android Applications Tutorial
20-a. Notification


20.1 Notifications

We now know whatActivitiesandIntentsare. Time to move on to services. However, since services mostly interact with a user through notifications, we need to first understand how a simple program to deal withNotifications.

What are Notifications?
They are a way of alerting a user about an event that he needs to be informed about or even take some action on getting that information.

Notification on Android can be done in any of the following ways:

  • Status Bar Notification
  • Vibrate
  • Flash lights
  • Play a sound

Through the Notification, we can allow the user to launch a new activity as well. Now we will look atstatus bar notificationsince this can be easily tested on the emulator.

To create a status bar notification, we'll need to use two classes:NotificationandNotificationManager.

  • Notification
    Defines the properties of the status bar notification like the icon to display, the test to display when the notification first appears on the status bar and the time to display.
  • NotificationManager
    NotificationManager is an android system service that executes and manages all notifications. Hence you cannot create an instance of the NotificationManager but you can retrieve a reference to it by calling thegetSystemService()method.

Once we got this handle, we invoke thenotify()method on it by passing the notification object created.

So far, we have all the information to display on the status bar. However, when the user clicks the notification icon on the status bar, what detailed information should we show the user?

This is yet to be created. This is done by calling the methodsetLatestEventInfo()on the notification object.

What needs to be passed to this method, we will see with an example.

  1. Get a handle to the NotificationManager:
    private NotificationManager mNotificationManager;mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  2. Create a notification object along with properties to display on the status bar
    final Notification notifyDetails = new Notification(R.drawable.android,"You've got a new notification!",System.currentTimeMillis());
  3. Add the details that need to get displayed when the user clicks on the notification. In this case, I have created an intent to invoke the browser to show the website http://www.android.com
    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(NotificationsA.this, 0,         notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);        notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
  4. Now the stage is set. Notify.
    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
    Note that all of the above actions(except getting a handle to theNotificationManager) are done on the click of a buttonStart Notification. So all the details go into thesetOnClickListener()method of the button. Similarly, the notification, for the example sake is stopped by clicking a cancel notification button. And the code there is :
    mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);

Now, you may realize that the constantSIMPLE_NOTIFICATION_IDbecomes the way of controlling, updating, stopping a current notification that is started with the same ID.

Here is our Java code,NotificationA.java:

package com.bogotobogo.notificationsa;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 NotificationsA extends Activity {private NotificationManager mNotificationManager;private int SIMPLE_NOTFICATION_ID;    @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,"You've got a new notification!",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(NotificationsA.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);        }        });    }}




Files used in this Notification example,NotificationsA.zip


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 判断Android(安卓)APP是否在前台运行
  2. Error parsing XML: unbound prefix on X
  3. [置顶] EditText属性大全
  4. android控件常用属性区别
  5. Android(安卓)利用BroadcastReceiver实时
  6. Android 中调试手段 打印函数调用栈信息
  7. Android处理ListView的条目长按事件
  8. android jetpack 简单livedata和viewmode
  9. RxJava1的使用介绍
  10. Android 4.0源码放出