使用步骤:
1)创建自定义视图
2)获取远程视图对象(注:Notification的contentView不能为空)
3)设置PendingIntent(来响应各种事件)
4)发起Notification
大体4步骤这里就不详细说了,下面就把DEMO中的列子拿出来说下
1 Activity 中的代码
/** * Notification的使用 * @author root * */ public class NotificationActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification); Button btn1 = (Button) findViewById( R.id.btn1); btn1.setOnClickListener( new BtnListener()); Button btnCostom2 = (Button) findViewById( R.id.btn_costom2); btnCostom2.setOnClickListener( new BtnListener()); } class BtnListener implements OnClickListener{ @Override public void onClick(View v) { Integer id = v.getId(); switch ( id ) { case R.id.btn1: notifition1(); break; case R.id.btn_costom2: showCustomizeNotification(); break; default: break; } } } /** * Android 默认的notification */ private void notifition1(){ Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( NotificationActivity.this).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("new message title") .setContentText("wzt@android.com内容的主体"); mBuilder.setTicker("New message");//第一次提示消息的时候显示在通知栏上 mBuilder.setNumber(12); mBuilder.setLargeIcon(btm); mBuilder.setAutoCancel(true);//自己维护通知的消失 //构建一个Intent,点击通知栏,则跳转到 MainActivity上 Intent resultIntent = new Intent(NotificationActivity.this, MainActivity.class); //封装一个Intent PendingIntent resultPendingIntent = PendingIntent.getActivity( NotificationActivity.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); // 设置通知主题的意图 mBuilder.setContentIntent(resultPendingIntent); //获取通知管理器对象 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build()); } /** * 自定义 Notification的布局 */ private void showCustomizeNotification(){ CharSequence title = "i am new"; int icon = R.drawable.ic_launcher; long when = System.currentTimeMillis(); Notification noti = new Notification(icon, title, when + 10000); noti.flags = Notification.FLAG_INSISTENT; // 1、创建一个自定义的消息布局 view.xml // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段 RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification_costom); remoteView.setImageViewResource(R.id.image, R.drawable.ic_launcher); remoteView.setTextViewText(R.id.text , "通知类型为:自定义View"); noti.contentView = remoteView; // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法) //这儿点击后简单启动Settings模块 (Activity) PendingIntent contentIntent = PendingIntent.getActivity (NotificationActivity.this, 0,new Intent("android.settings.SETTINGS"), 0); noti.contentIntent = contentIntent; NotificationManager mnotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mnotiManager.notify(1, noti); } }
2 Activity 对应的布局
<?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:orientation="vertical" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通知1" /> <Button android:id="@+id/btn_costom2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通知2" /> </LinearLayout>
3 自定义的notification的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_toRightOf="@+id/image" android:layout_height="wrap_content" android:textColor="#000" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_toRightOf="@+id/image" android:layout_height="wrap_content" android:textColor="#000" android:text="点击跳转"/> <ProgressBar android:id="@+id/progress_horizontal" style="?android:attr/progressBarStyleHorizontal" android:layout_below="@+id/text" android:layout_toRightOf="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="75" /> </RelativeLayout>
附件是网上大神写的例子,弄过来做个备份以后好用。
相关推荐
toast 和notification 的学习 很适合初学android的
这个是android notification框架,如有需要请下载。发发发
了解Android Notification的基本用法
Notification是几个有用的实例
一个关于安卓toast和notification使用方法的Android源码,来自eoeandroid社区,对初学者或许是个帮助吧。
本实例描述的Android开发中的Notification通知控件的应用,包括通知的创建、属性设置、跳转方法以及删除等。希望能给学习Notification通知的朋友带来帮助。
使用android做的小例子 可以在通知栏显示消息和小图标 点击进去可以进入一个页面 主要用于notification的学习 基于android2.3开发
演示Android平台上的Notification的简单使用。如有不足,请指正,我们一起学习进步!谢谢!
自学 Android 做的第二十七个 Android 程序,使用了 NotificationManager、Notification 和 Notification.Builder
xamarin学习笔记A12(安卓Notification),当一个应用程序不在前台运行而在后台运行时,这时有个消息希望让用户知道,那么可以在状态栏显示一个消息图标
和博客得配套看http://blog.csdn.net/yangjianbo456/article/details/50478180 博客名:Notification通知栏在app更新时自定义布局与原生态的对比分析(自定义的布局会卡死!); 可以大家一起讨论,互相学习。
Android 通知(notification)简单实用Demo,包含点击功能简单例子,供大家下载学习
android 源码学习
你是不是一个android学习 开发人员 是否厌恶findViewById id ; 还是不停重复是去写 xutils框架[4大模块] ViewUtils模块基于注解完美的简化这个问题 从此告别findViewById id 在这个简单的通知demo中 我只是简单的用...
状态栏Notification简单Demo.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
这个是通知栏框架(Notificaiton)的全面学习,里面把大概所有的情况都列了出来,通过一个DEMO让你了解它的大致所有使用过程。 可以通过以下博文进行配套了解(有效果图): ...
Eclipse的关于通知notification的小应用,源码有注释,供参考.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Notification通知DEMO 仅供大家学习
NULL 博文链接:https://forlan.iteye.com/blog/2264722
这个是通知栏框架(Notificaiton)的全面学习,里面把大概所有的情况都列了出来,通过一个DEMO让你了解它的大致所有使用过程。 可以通过以下博文进行配套了解(有效果图): ...