使用 Notification 在状态栏中显示一个通知。
来源:互联网 发布:孢子中文版for mac 编辑:程序博客网 时间:2024/06/07 22:33
notification是一种出现在状态栏的提示,效果如下:单击send 按钮,状态栏出现消息通知图标;下拉状态栏,出现消息内容;点击消息内容时,返回 MainActivty。
layout_main.xml 布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_horizontal" tools:context="com.android.notificationtest.MainActivity" > <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send"/> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="cancel"/></LinearLayout>
item.xml 布局文件:
由于消息提示默认为白色,与字体颜色相冲突,所以,修改背景颜色(此处为黑色)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="horizontal" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout></LinearLayout>
MainActivity :
package com.android.notificationtest;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RemoteViews;public class MainActivity extends Activity {private static final int NOTIFICATION_ID = 0X123;private NotificationManager manager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button send = (Button)findViewById(R.id.send);Button cancel = (Button)findViewById(R.id.cancel);manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);send.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {addMessage();}});cancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {cancelMessage();}});}/** * 增加消息 */protected void addMessage() {// 创建一个 NotificationCompat 类 // Builder class for NotificationCompat objectsNotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this); mBuilder.setSmallIcon(R.drawable.sha); // 系统默认的和自定义的都不能少// 系统默认的//mBuilder.setContentTitle("你有一条消息:");// 系统默认的//mBuilder.setContentText("你的消息内容为......");// 结合所有已设置的选项返回一个新的 Notification(通知) 对象Notification notification = mBuilder.build();// 自定义(开始)// 通过指定的布局文件新建一个RemoteViews对象。//(RemoteViews一个描述了可以在其他进程中显示的view层)RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.item); remoteViews.setImageViewResource(R.id.imageView, R.drawable.zhu); remoteViews.setTextViewText(R.id.title, "标题的题目......"); remoteViews.setTextViewText(R.id.text, "内容是什么......"); notification.contentView = remoteViews; // 自定义结束// 通知消息: 声音 + 振动notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;// 通知时间notification.when = System.currentTimeMillis();// 当用户点击通知栏的Notification时候,切换回MainActivity。 // PendingIntent 通常会传给其他的应用组件,从而由其他应用程序来执行 PendingIntent 所包装的"Intent"Intent intent = new Intent(this, MainActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);notification.contentIntent = pi; // 在状态栏中显示一个通知。manager.notify(NOTIFICATION_ID, notification);}/** * 删除消息 */protected void cancelMessage() {manager.cancel(NOTIFICATION_ID);}}
PendingIntent 包装的 Intent 每次都要 new 出来,增加了内存的消耗,可以采用 singleTop 模式(当将要启动的目标 Activity 已经位于 Task 栈顶时,系统不会重新创建目标 Activity 的实例,而是直接复用已有的 Activity 实例):
android:launchMode="singleTop"
0 0
- 使用 Notification 在状态栏中显示一个通知。
- 使用Notification在状态栏上通知
- 在ios5 中 使用通知机制(notification ) 来显示一个消息(banner 和 alert)
- 【Android开发】消息提示框与对话框-使用Notification在状态栏上显示通知
- 使用Notication在状态栏上显示通知
- 在状态栏显示通知
- 【notification】Android 中创建状态栏通知
- Android状态栏通知Notification使用小结
- android Notification 状态栏通知使用示例
- 创建状态栏通知 notification
- Notification(状态栏通知)
- Notification状态栏通知
- 发送状态栏通知 Notification
- Android 状态栏通知Notification
- 【Android】状态栏通知Notification
- Android Notification状态栏通知
- Notification(状态栏通知)详解
- Notification(状态栏通知)学习
- 用心做点事,写点代码的哲学(编程的基本功)
- Spring中depends-on的作用是什么?
- csdn markdown 的使用
- 支持向量机(三)核函数
- spring配置packagesToScan的问题
- 使用 Notification 在状态栏中显示一个通知。
- 支持向量机SVM(四)
- 数据库存取图片
- Access denied for user ''@'localhost' to database 'xxx‘
- [Built-in Functions] - P
- Qt使用教程之指定运行设置(二)
- C:fahr2celsius
- 关于activiti的判断点的特殊问题
- Linux tar命令