android2.3 api demo 学习系列(21)--App/Notification/Incoming Message

现在我们开始学习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里面有详细的注释说明,能很轻松的理解代码的意图。

我们来看下效果图:

android2.3 api demo 学习系列(21)--App/Notification/Incoming Message_第1张图片
 android2.3 api demo 学习系列(21)--App/Notification/Incoming Message_第2张图片

相关知识:

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);
 

 

 

你可能感兴趣的:(android,api,demo,notification)