Android中Notification的使用以及自定义

来源:互联网 发布:呼伦贝尔盟616事件知乎 编辑:程序博客网 时间:2024/05/22 00:16

界面很简单:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    style="@style/all" >    <Button        style="@style/all"        android:onClick="onclick"        android:text="通知" />    <Button        style="@style/all"        android:onClick="onclick2"        android:text="自定义通知" /></LinearLayout>

content.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" >    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/ic_launcher" /></LinearLayout>



以下是主体的代码:

MainActivity.java

package com.example.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.RemoteViews;import android.widget.RemoteViews.RemoteView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void onclick(View v) {// 1.得到通知管理器NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 2.构建通知Notification noti = new Notification(android.R.drawable.stat_notify_chat, "这是提示消息 ",System.currentTimeMillis());// 3.设置通知栏点击事件Intent itent = new Intent(this, MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 100,itent, 0);noti.setLatestEventInfo(this, "通知的标题", "通知的内容", pendingIntent);noti.flags = Notification.FLAG_AUTO_CANCEL;// 点击后自动消失// 4.发送通知notificationManager.notify(100, noti);}public void onclick2(View v) {// 1.得到通知管理器NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 2.构建通知Notification noti = new Notification();noti.tickerText = "拦截到了N条信息";noti.icon = R.drawable.ic_launcher;RemoteViews conteView = new RemoteViews(getPackageName(),R.layout.content);noti.contentView = conteView;//通知显示的布局Intent itent = new Intent(this, OtherActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 100,itent, 0);noti.contentIntent = pendingIntent;//点击事件noti.flags = Notification.FLAG_AUTO_CANCEL;// 点击后自动消失// 4.发送通知notificationManager.notify(100, noti);}}


OtherActivity.java

package com.example.notification;import android.app.Activity;import android.os.Bundle;public class OtherActivity extends Activity{protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.content);}}




















原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 退差价把红包退还了怎么办 网上购票票丢了怎么办 岗位人手不够老板又不招人来怎么办 辞职后提成不发怎么办 老板给客户吵架员工该怎么办 冰柜声音大怎么办嗡嗡响 交了钱电没有怎么办 小白熊电动吸奶器显示F1怎么办 花洒的水变小了怎么办 手机插卡处坏了怎么办 吉利帝豪一键启动钥匙没电怎么办 居民医保断交5年怎么办 社保和医保断了怎么办 停缴了两年社保怎么办 医保交不够20年怎么办 医保断交4个月怎么办 医保断交5个月怎么办 换城市后社保卡怎么办 苹果6s呼叫失败怎么办 苹果手机打电话显示呼叫失败怎么办 苹果手机经常呼叫失败怎么办 苹果手机呼叫号码呼叫失败怎么办 好友把我拉黑了怎么办 有人一直打骚扰电话怎么办 每天都有骚扰电话怎么办 苹果7被电话轰炸怎么办 苹果手机接听声音小怎么办 微信账号封了怎么办 在京东买的手机出现问题怎么办 销售没胆量扫楼怎么办 骨龄比实际年龄小怎么办 身体年龄比实际年龄大怎么办 吃了不熟的鸡蛋怎么办 六角龙尾巴烂了怎么办 兰花长出来的包怎么办 长寿花花开败了怎么办 手机分期0首付怎么办办 办分期手机掉了怎么办 手机办分期被骗了怎么办 办手机分期年龄不够怎么办 信美分期没额度怎么办