android 组件Notification实例
来源:互联网 发布:那个网络日语培训班好 编辑:程序博客网 时间:2024/05/11 00:51
本例是对Notification的练习,通过点击按钮弹出Notification系统通知。
主要知识点:
//获取系统通知管理器
private NotificationManager notificationManager;
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//实例化一个Notification
Notification notification = new Notification(R.drawable.ic_launcher,"tickerText", System.currentTimeMillis());
函数解释:
android.app.Notification.Notification(int icon,CharSequence tickerText,long when)
Deprecated. Use Builder
instead.
Constructs a Notification object with the information needed to have a status bar icon without the standard expanded view.
- Parameters:
- icon The resource id of the icon to put in the status bar.通知图标
- tickerText The text that flows by in the status bar when the notification first activates.(看截图就明白了)
- when The time to show in the time field. In the System.currentTimeMillis timebase.系统时间
//设置Notification的信息
notification.setLatestEventInfo(AtyNotification.this, "contentTitle",
"contentText", PendingIntent.getActivity(AtyNotification.this,
1, getIntent(), 0));
函数解释:
void android.app.Notification.setLatestEventInfo(Context context,CharSequence contentTitle,CharSequence contentText,PendingIntent contentIntent)
Deprecated. Use Builder
instead.
Sets the contentView
field to be a view with the standard "Latest Event" layout.
Uses the icon
and when
fields to set the icon and time fields in the view.
- Parameters:
- context The context for your application / activity.
- contentTitle The title that goes in the expanded entry.
- contentText The text that goes in the expanded entry.
- contentIntent The intent to launch when the user clicks the expanded notification. If this is an activity, it must include the
android.content.Intent.FLAG_ACTIVITY_NEW_TASK
flag, which requires that you take care of task management as described in theTasks and Back Stack document. - 点击通知后触发的Intent,进入的activity。
布局文件:notification_layout.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:orientation="vertical" >
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showNotification"
android:text="弹出通知" />
</LinearLayout>
AtyNotification.java文件:
package com.fxj.composit;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import com.fxj.compractice.R;
public class AtyNotification extends Activity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
// 获取系统通知管理器
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 当再次点击系统通知进入此activity时取消通知。
if (notificationManager != null) {
notificationManager.cancel(R.layout.notification_layout);
}
}
@SuppressWarnings("deprecation")
public void showNotification(View view) {
// 实例化一个Notification
Notification notification = new Notification(R.drawable.ic_launcher,
"tickerText", System.currentTimeMillis());
// 设置Notification的信息
notification.setLatestEventInfo(AtyNotification.this, "contentTitle",
"contentText", PendingIntent.getActivity(AtyNotification.this,
1, getIntent(), 0));
//执行Notification
notificationManager.notify(R.layout.notification_layout, notification);
}
}
运行效果:
结束。
- android 组件Notification实例
- Android Notification实例理解
- Android Notification实例理解
- android Notification 实例
- Android当中Notification实例
- android 之 Notification 实例
- Android组件11—Notification
- Android组件11—Notification
- android Notification的实例与Notification API
- android-UI组件实例大全(十三)------消息提示框:Toast和Notification
- android-UI组件实例大全(十三)------消息提示框:Toast和Notification
- Android的Notification实例介绍
- android sdk --- Notification用法实例
- android四大组件之一notification(广播)
- Service组件+Notification断点续传下载(Android)
- android 组件Spinner组件实例
- android自定义Notification通知栏实例
- Android 监听Notification 被清除实例代码
- 位域(位段)
- openssl的多平台编译
- eclipse导入项目,项目名出现红叉的情况
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
- noip2008初赛-找第K大的数
- android 组件Notification实例
- 空格替换(Java)
- 2044 一只小蜜蜂...【递推】
- Swing 美化工具包
- 仿QQ5.0侧滑菜单实现
- 女排世界杯中国夺两连胜 3-0横扫阿尔及利亚
- 心理学之舒适区、学习区、恐慌区
- 数据窗口字段保存显示超过255个字符
- noip2009初赛-最大连续子段和