Android Notification 通知栏
2016-05-15 19:34
344 查看
private void showNotification() { // 消息通知栏 // 定义NotificationManager String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); // 定义通知栏展现的内容信息 int icon = R.drawable.ic_launcher; CharSequence tickerText = "我的通知栏标题"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); // 定义下拉通知栏时要展现的内容信息 Context context = getApplicationContext(); CharSequence contentTitle = "我的通知栏标展开标题"; CharSequence contentText = "我的通知栏展开详细内容"; Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); // 用mNotificationManager的notify方法通知用户生成标题栏消息通知 mNotificationManager.notify(1, notification); }
private void showNotification() { NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(this, MainActivity.class); PendingIntent pd = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); // 自定义下拉视图,比如下载软件时,显示的进度条。 Notification notification = new Notification(); notification.icon = R.drawable.ic_launcher; notification.tickerText = "Custom!"; RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notify_view); contentView.setImageViewResource(R.id.imageView_icon, R.drawable.ic_launcher); contentView.setTextViewText(R.id.textView_title, "湖北新闻广播"); contentView.setTextViewText(R.id.textView_msg, "播放中"); notification.contentView = contentView; // 使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法 // 但是必须定义 contentIntent notification.contentIntent = pd; nm.notify(3, notification); // nm.cancel(id); }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal" android:padding="3dp" > <ImageView android:id="@+id/imageView_icon" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_marginRight="10dp" /> <RelativeLayout android:id="@+id/relativeLayout_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/imageView_icon" > <TextView android:id="@+id/textView_title" android:layout_width="100dp" android:layout_height="wrap_content" android:textColor="#FFF" /> <TextView android:id="@+id/textView_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView_title" /> </RelativeLayout> <ImageButton android:id="@+id/button_notification_play" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@color/transparent" android:src="@drawable/play" /> </RelativeLayout>
相关文章推荐
- 各种Android插件
- Android应用资源---字符串资源类型(String)
- android 滚动条
- Android SQLite 操作
- Android文件上传,PHP端接收
- Android学习路线指南
- Android TextView drawableLeft 在代码中实现
- Android APK反编译详解(附图)【转】
- Android再按一次退出软件
- Android 如何让EditText不自动获取焦点
- Android Socket 的使用
- Android Dialog 对话框
- Android 去掉自定义dialog的白色边框
- android自定义滑动开关组件【转】
- Android静态,动态广播示例
- Android 图片压缩也即生成缩略图方法
- Android应用开发提高篇(4)-----Socket编程(多线程、双向通信)
- Android开发之:如何使用样式和主题
- Android布局属性详解
- 【转】Android Gesture 之触摸屏手势识别