Android之开发消息通知栏
程序员文章站
2022-05-20 21:45:35
一:先来效果图
二:实现步骤
1.xml布局实现
一:先来效果图
二:实现步骤
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(); } }
简单粗暴实用,你值得拥有
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
上一篇: 食醋的作用有哪些
下一篇: 秦始皇说了什么?让他的陵墓千年没人敢动?