现在我们开始学习android的Status Bar Notifications相关内容

1、首先我们来实现一个Notification:在status bar添加一个图片和信息,并让手机震动。用户打开status bar看到更详细的信息,点击该Notification打开一个activity

首先我们来看实现代码:

 protected void showNotification() {        // look up the notification manager service        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);        // The details of our fake message        CharSequence from = "angie";        CharSequence message = "今天加班记得订饭啊。。。";        // The PendingIntent to launch our activity if the user selects this notification        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,                new Intent(this, IncomingMessageView.class), 0);        // The ticker text, this uses a formatted string so our message could be localized        String tickerText = getString(R.string.app_notification_incomingmessage_view_msg1, message);        // construct the Notification object.        Notification notif = new Notification(R.drawable.main, tickerText,                System.currentTimeMillis());        // Set the info for the views that show in the notification panel.        notif.setLatestEventInfo(this, from, message, contentIntent);        // after a 100ms delay, vibrate for 250ms, pause for 100 ms and        // then vibrate for 500ms.        notif.vibrate = new long[] { 100, 250, 100, 500};        // Note that we use R.layout.incoming_message_panel as the ID for        // the notification.  It could be any integer you want, but we use        // the convention of using a resource id for a string related to        // the notification.  It will always be a unique number within your        // application.        nm.notify(R.string.app_notification_incomingmessage_view_msg1, notif);    }

apidemo里面有详细的注释说明,能很轻松的理解代码的意图。

我们来看下效果图:


相关知识:

1、sdk中关于notification的说明

我们可以使用activity和service来创建一个notification,由于activity可以和用户交互,一般我们更多的是service来使用notification提醒用户。

我们通过Notification的实例来配置notification的图片,信息,intent以及一些其他的附加信息。android的系统是使用NotificationManager来执行和管理Notification,我们不能直接的实例化NotificationManager,必须通过getSystemService()来获取它的引用。将我们得Notification通过notify()传递进去。下面我们来看下创建一个notification的步骤

1)获得NotificationManager引用:

String ns = Context.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
2)实例化
Notification:
int icon = R.drawable.notification_icon;CharSequence tickerText = "Hello";long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText, when);
3) 定义notification的 message 和PendingIntent:
Context context = getApplicationContext();CharSequence contentTitle = "My notification";CharSequence contentText = "Hello World!";Intent notificationIntent = new Intent(this, MyClass.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
4)将notification递交给NotificationManager
private static final int HELLO_ID = 1;mNotificationManager.notify(HELLO_ID, notification);
2、管理notification 我们通过 NotificationManager.notify(int, Notification)交付我们得notification给NotificationManager。全第一个参数为notification的唯一标示,第二个参数为notification对象。我们可以通过第一个参数来修改删除notification。 3、修改更新notification。通过再次调用setLateEventInfo方法可以修改除了上下文中得标题文字外的所有notification属性。修改后再次notify()就ok 4、自定义notification的layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/layout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="10dp" >    <ImageView android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:layout_alignParentLeft="true"        android:layout_marginRight="10dp" />    <TextView android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/image"        style="@style/NotificationTitle" />    <TextView android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/image"        android:layout_below="@id/title"        style="@style/NotificationText" /></RelativeLayout>
我们注意到textview的style属性,在版本2.3之前我们可以这么定义
<?xml version="1.0" encoding="utf-8"?><resources>    <style name="NotificationText">      <item name="android:textColor">?android:attr/textColorPrimary</item>    </style>    <style name="NotificationTitle">      <item name="android:textColor">?android:attr/textColorPrimary</item>      <item name="android:textStyle">bold</item>    </style>    <!-- If you want a slightly different color for some text,         consider using ?android:attr/textColorSecondary --></resources>
2.3之后:
<?xml version="1.0" encoding="utf-8"?><resources>    <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />    <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" /></resources>
代码中加载自定义样式
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);contentView.setImageViewResource(R.id.image, R.drawable.notification_image);contentView.setTextViewText(R.id.title, "Custom notification");contentView.setTextViewText(R.id.text, "This is a custom layout");notification.contentView = contentView;
自定义notification中无需再调用setLatestEventInfo
Intent notificationIntent = new Intent(this, MyClass.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.contentIntent = contentIntent;mNotificationManager.notify(CUSTOM_VIEW_ID, notification);

更多相关文章

  1. android平台6410背光修改
  2. 自定义Dialog之信息提示
  3. Snackbar使用,修改字体和背景颜色
  4. Ubuntu14.04 下面安装SVN服务器
  5. Android系统密度的修改
  6. Android(安卓)SharedPreferences保存Map集合
  7. Android未接电话(未接电话个数,以及未接电话信息的读取)
  8. Android(安卓)button, xml文件定义形状,代码中修改背景颜色
  9. android使用ffmpeg的实例程序

随机推荐

  1. [下载]Android 手机十大必备软件
  2. android通过Base64往服务器上传图片和对
  3. 2017.03.12 Android卡顿分析
  4. Android 蓝牙模块
  5. Android 开发艺术探索笔记之二 -- IPC 机
  6. android 高德地图API 之 java.lang.Unsat
  7. Android下Affinities和Task
  8. Android socket 开发中遇到的问题.
  9. Android(安卓)初级面试者拾遗(前台界面篇)
  10. Android的MediaRecorder框架介绍