android2.--App/Notification 以及android.app.Notification$Builder.setProgress问题

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

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

首先我们来看实现代码:

 

Java代码   收藏代码
  1. protected void showNotification() {  
  2.         // look up the notification manager service  
  3.         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  4.   
  5.         // The details of our fake message  
  6.         CharSequence from = "angie";  
  7.         CharSequence message = "今天加班记得订饭啊。。。";  
  8.   
  9.         // The PendingIntent to launch our activity if the user selects this notification  
  10.         PendingIntent contentIntent = PendingIntent.getActivity(this0,  
  11.                 new Intent(this, IncomingMessageView.class), 0);  
  12.   
  13.         // The ticker text, this uses a formatted string so our message could be localized  
  14.         String tickerText = getString(R.string.app_notification_incomingmessage_view_msg1, message);  
  15.   
  16.         // construct the Notification object.  
  17.         Notification notif = new Notification(R.drawable.main, tickerText,  
  18.                 System.currentTimeMillis());  
  19.   
  20.         // Set the info for the views that show in the notification panel.  
  21.         notif.setLatestEventInfo(this, from, message, contentIntent);  
  22.   
  23.         // after a 100ms delay, vibrate for 250ms, pause for 100 ms and  
  24.         // then vibrate for 500ms.  
  25.         notif.vibrate = new long[] { 100250100500};  
  26.   
  27.         // Note that we use R.layout.incoming_message_panel as the ID for  
  28.         // the notification.  It could be any integer you want, but we use  
  29.         // the convention of using a resource id for a string related to  
  30.         // the notification.  It will always be a unique number within your  
  31.         // application.  
  32.         nm.notify(R.string.app_notification_incomingmessage_view_msg1, notif);  
  33.     }  

 

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

我们来看下效果图:


 

相关知识:

1、sdk中关于notification的说明

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

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

1)获得 NotificationManager引用:

Java代码   收藏代码
  1. String ns = Context.NOTIFICATION_SERVICE;  
  2. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);  
2)实例化  Notification:
Java代码   收藏代码
  1. int icon = R.drawable.notification_icon;  
  2. CharSequence tickerText = "Hello";  
  3. long when = System.currentTimeMillis();  
  4.   
  5. Notification notification = new Notification(icon, tickerText, when);  
 3) 定义notification的 message 和 PendingIntent:
Java代码   收藏代码
  1. Context context = getApplicationContext();  
  2. CharSequence contentTitle = "My notification";  
  3. CharSequence contentText = "Hello World!";  
  4. Intent notificationIntent = new Intent(this, MyClass.class);  
  5. PendingIntent contentIntent = PendingIntent.getActivity(this0, notificationIntent, 0);  
  6.   
  7. notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);  
 4)将notification递交给NotificationManager
Java代码   收藏代码
  1. private static final int HELLO_ID = 1;  
  2.   
  3. 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
Xml代码   收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/layout"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:padding="10dp" >  
  6.     <ImageView android:id="@+id/image"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="fill_parent"  
  9.         android:layout_alignParentLeft="true"  
  10.         android:layout_marginRight="10dp" />  
  11.     <TextView android:id="@+id/title"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_toRightOf="@id/image"  
  15.         style="@style/NotificationTitle" />  
  16.     <TextView android:id="@+id/text"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_toRightOf="@id/image"  
  20.         android:layout_below="@id/title"  
  21.         style="@style/NotificationText" />  
  22. </RelativeLayout>  
 我们注意到textview的style属性,在版本2.3之前我们可以这么定义
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="NotificationText">  
  4.       <item name="android:textColor">?android:attr/textColorPrimary</item>  
  5.     </style>  
  6.     <style name="NotificationTitle">  
  7.       <item name="android:textColor">?android:attr/textColorPrimary</item>  
  8.       <item name="android:textStyle">bold</item>  
  9.     </style>  
  10.     <!-- If you want a slightly different color for some text,  
  11.          consider using ?android:attr/textColorSecondary -->  
  12. </resources>  
 2.3之后:
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />  
  4.     <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />  
  5. </resources>  
 代码中加载自定义样式
Java代码   收藏代码
  1. RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);  
  2. contentView.setImageViewResource(R.id.image, R.drawable.notification_image);  
  3. contentView.setTextViewText(R.id.title, "Custom notification");  
  4. contentView.setTextViewText(R.id.text, "This is a custom layout");  
  5. notification.contentView = contentView;  
 自定义notification中无需再调用setLatestEventInfo
Java代码   收藏代码
  1. Intent notificationIntent = new Intent(this, MyClass.class);  
  2. PendingIntent contentIntent = PendingIntent.getActivity(this0, notificationIntent, 0);  
  3. notification.contentIntent = contentIntent;  
  4. mNotificationManager.notify(CUSTOM_VIEW_ID, notification);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值