一:先来效果图
二:实现步骤
1.xml布局实现
<?xml version="1.0" encoding="utf-8"?> <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="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="edu.feicui.notification.MainActivity"> <Button android:id="@+id/btn_create" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知" android:textSize="25sp" /> </LinearLayout>
2.activity的实现
package edu.feicui.notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RemoteViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
/**
* 通知栏Notification
*/
private NotificationManager mManager;
private Notification mNotification;
private PendingIntent mIntent;
private String cll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cll = "今年27号过年哟!";
ButterKnife.bind(this);
}
@Override
public void onContentChanged() {
super.onContentChanged();
init();
}
private void init() {
//初始化通知栏管理者
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//意图数组
Intent[] intents = {new Intent(this, NotificationAcitivity.class)};
//待处理意图对象
mIntent = PendingIntent.getActivities(this, 0, intents, 0);
//消息栏通知对象
mNotification = new Notification();
}
@OnClick(R.id.btn_create)
public void create() {
//设置在通知栏的消息图标
mNotification.icon = R.mipmap.logo_new;
//设置在通知栏的信息内容
mNotification.tickerText = "重大消息";
//设置默认的声音,此外还可以设置震动(需加入权限)
mNotification.defaults = Notification.DEFAULT_SOUND;
//添加灯光
// mNotification.defaults=Notification.DEFAULT_LIGHTS;
//不能删除
mNotification.flags = Notification.FLAG_NO_CLEAR;
//设置下拉时的显示布局
RemoteViews convertView = new RemoteViews(getPackageName(), R.layout.layout_content);
convertView.setImageViewResource(R.id.img, R.mipmap.logo_new);
convertView.setTextViewText(R.id.txt, cll);
mNotification.contentView = convertView;
mNotification.contentIntent = mIntent;
//发送通知
// 第一个参数唯一的标识该Notification,第二个参数就是Notification对象
mManager.notify(1, mNotification);
}
}
3.AndroidManifest添加权限
<uses-permission android:name="android.permission.VIBRATE"/>
4.跳转界面的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:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000" android:textSize="20dp" android:text="今年27号过年哟!" /> </LinearLayout>
5.跳转activity的实现
package edu.feicui.notification;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by Administrator on 2017-1-20.
*/
public class NotificationAcitivity extends Activity {
private NotificationManager mManager;
private int index = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
//初始化通知栏管理者
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
index = 2;
mManager.cancelAll();
}
}
简单粗暴实用,你值得拥有
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
# android
# 消息通知
# android通知栏消息
# android发送通知消息
# Android中通过Notification&NotificationManager实现消息通知
# Android编程实现google消息通知功能示例
# Android消息通知栏的实现方法介绍
# Android自定义Notification添加点击事件
# Android中AlarmManager+Notification实现定时通知提醒功能
# Android 中Notification弹出通知实现代码
# Android编程使用Service实现Notification定时发送功能示例
# Android 通知使用权(NotificationListenerService)的使用
# android使用NotificationListenerService监听通知栏消息
# Android消息通知Notification常用方法(发送消息和接收消息)
# 跳转
# 还可以
# 第一个
# 第二个
# 先来
# btn_create
# id
# Button
# textSize
# wrap_content
# MainActivity
# paddingLeft
# activity_horizontal_margin
# activity_vertical_margin
# paddingRight
# feicui
# notification
# paddingTop
# context
# Intent
相关文章:
网站制作服务平台,有什么网站可以发布本地服务信息?
建站三合一如何选?哪家性价比更高?
,购物网站怎么盈利呢?
红河网站制作公司,红河事业单位身份证如何上传?
浅谈Javascript中的Label语句
图册素材网站设计制作软件,图册的导出方式有几种?
c# 在高并发下使用反射发射(Reflection.Emit)的性能
高端云建站费用究竟需要多少预算?
,网站推广常用方法?
php json中文编码为null的解决办法
制作营销网站公司,淘特是干什么用的?
武汉网站制作费用多少,在武汉武昌,建面100平方左右的房子,想装暖气片,费用大概是多少啊?
内网网站制作软件,内网的网站如何发布到外网?
定制建站流程解析:需求评估与SEO优化功能开发指南
如何选择网络建站服务器?高效建站必看指南
公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?
如何快速登录WAP自助建站平台?
如何快速生成ASP一键建站模板并优化安全性?
Python如何创建带属性的XML节点
企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?
制作网页的网站有哪些,电脑上怎么做网页?
如何确认建站备案号应放置的具体位置?
如何通过.red域名打造高辨识度品牌网站?
如何配置FTP站点权限与安全设置?
如何快速上传自定义模板至建站之星?
浅析上传头像示例及其注意事项
网站好制作吗知乎,网站开发好学吗?有什么技巧?
建站之星上传入口如何快速找到?
建站之星CMS建站配置指南:模板选择与SEO优化技巧
建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析
公司网站设计制作厂家,怎么创建自己的一个网站?
建站为何优先选择香港服务器?
c# F# 的 MailboxProcessor 和 C# 的 Actor 模型
建站主机默认首页配置指南:核心功能与访问路径优化
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
制作网站公司那家好,网络公司是做什么的?
微信小程序 input输入框控件详解及实例(多种示例)
建站上传速度慢?如何优化加速网站加载效率?
建站主机SSH密钥生成步骤及常见问题解答?
高端建站如何打造兼具美学与转化的品牌官网?
香港服务器网站推广:SEO优化与外贸独立站搭建策略
焦点电影公司作品,电影焦点结局是什么?
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
网页设计与网站制作内容,怎样注册网站?
名字制作网站免费,所有小说网站的名字?
网站制作企业,网站的banner和导航栏是指什么?
如何在VPS电脑上快速搭建网站?
贸易公司网站制作流程,出口贸易网站设计怎么做?
如何在香港免费服务器上快速搭建网站?
详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)
*请认真填写需求信息,我们会在24小时内与您取得联系。