欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中通过Notification&NotificationManager实现消息通知

程序员文章站 2023-11-20 16:28:46
notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(broadcast receiver,service和不活跃的act...

notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(broadcast receiver,service和不活跃的activity)警示用户有需要注意的事件发生的最好途径。

  1、新建一个android项目

    我新建项目的 minsdkversion="11",targetsdkversion="19"。也就是支持最低版本的3.0的。

  2、习惯性地打开项目清单文件androidmanifest.xml,添加一个权限:<uses-permission android:name="android.permission.vibrate"/> 不添加不行的。

  3、在布局activity_main.xml中添加几个按钮,样子就大概这样,垂直排版的linearlayout

具体代码

<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:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  tools:context=".mainactivity" >
    <button 
      android:id="@+id/btn_01"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="3.0以前版本的notification,用新的吧"
      android:onclick="click"
      />
    <button 
      android:id="@+id/btn_02"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="大视图文本通知"
      android:onclick="click"
      />
    <button 
      android:id="@+id/btn_03"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="大视图图片通知"
      android:onclick="click"
      />
    <button 
      android:id="@+id/btn_04"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="进度条通知"
      android:onclick="click"
      />
</linearlayout>

Android中通过Notification&NotificationManager实现消息通知   

4、mainactivity中的代码:

 package com.xin.day__notificationdemo;
  import java.util.timer;
  import java.util.timertask;
  import android.app.activity;
  import android.app.notification;
  import android.app.notificationmanager;
  import android.app.pendingintent;
 import android.content.intent;
 import android.graphics.bitmapfactory;
 import android.os.bundle;
 import android.support.v.app.notificationcompat;
 import android.support.v.app.notificationcompat.bigpicturestyle;
 import android.support.v.app.notificationcompat.bigtextstyle;
 import android.support.v.app.notificationcompat.builder;
 import android.util.log;
 import android.view.view;
 public class mainactivity extends activity {
   //通知的唯一标识,在一个应用程序中不同的通知要区别开来
   private static final int no = x;
   private static final int no = x;
   private static final int no = x;
   private static final int no = x;
   //进度条要用
   private int progress = ;
   @override
   protected void oncreate(bundle savedinstancestate) {
     super.oncreate(savedinstancestate);
     setcontentview(r.layout.activity_main);
   }
   //click方法,和xml文件中的各个按钮的onclick属性的值要一致
   public void click(view view) {
     //创建notificationmanager
     final notificationmanager manager = (notificationmanager) getsystemservice(notification_service);
     //用switch语句控制四个控件
     switch (view.getid()) {
     case r.id.btn_: {
       notification notification = new notification();
       notification.icon = r.drawable.ic_launcher;
       notification.tickertext = "有消息了。。。";
       intent intent = new intent(this, mainactivity.class);
       pendingintent pendingintent = pendingintent.getactivity(this, ,
           intent, pendingintent.flag_update_current);
       notification.setlatesteventinfo(this, ".以前的通知", "试试而已", pendingintent);
       notification.when = system.currenttimemillis();
       notification.defaults = notification.default_all;
       notification.flags = notification.flag_auto_cancel;
       notification.number = ;
       notification.vibrate = new long[]{, };
       manager.notify(no, notification);
     }
     break;
     case r.id.btn_:{
       //大视图文本通知
       //创建消息构造器,在扩展包
       notificationcompat.builder builder = new notificationcompat.builder(this);
       //设置当有消息是的提示,图标和提示文字
       builder.setsmallicon(r.drawable.ic_launcher).setticker("有新消息了");
       //需要样式
       bigtextstyle style = new bigtextstyle();
       style.setbigcontenttitle("上课通知");//通知的标题
       style.bigtext("今天下午要在综b上jsp");//通知的文本内容
       //大视图文本具体内容
       style.setsummarytext("这是正常的课程安排,请各位同学按时上课");
       builder.setstyle(style);
       //显示消息到达的时间,这里设置当前时间
       builder.setwhen(system.currenttimemillis());
       //获取一个通知对象
       notification notification = builder.build();
       notification.flags = notification.flag_auto_cancel;
       //发送(显示)通知
       //notify()第一个参数id an identifier for this notification unique within your application
       //get?意思说,这个通知在你的应用程序中唯一的标识符
       manager.notify(no, notification);
     }
     break;
     case r.id.btn_:{
       //大视图图片通知
       notificationcompat.builder builderpic = new builder(this);
       builderpic.setsmallicon(r.drawable.ic_launcher).setticker("新浪体育提醒");
       //进行设置
       bigpicturestyle picturestyle = new bigpicturestyle();
       picturestyle.setbigcontenttitle("新浪体育 快船vs骑士 ");
       picturestyle.bigpicture(bitmapfactory.decoderesource(getresources(), r.drawable.ic_game));
       picturestyle.setsummarytext(" 快船vs骑士 天王山之战!!!");//不要在意文字
       //设置样式
       builderpic.setstyle(picturestyle);
       //设置显示的时间
       builderpic.setwhen(system.currenttimemillis());
       notification notification = picturestyle.build();
       notification.flags = notification.flag_auto_cancel;
       //
       manager.notify(no, notification);
     }
     break;
     case r.id.btn_:{
       //进度条通知
       final notificationcompat.builder builderprogress = new notificationcompat.builder(this);
       builderprogress.setsmallicon(r.drawable.ic_launcher).setticker("进度条通知");
       builderprogress.setprogress(, progress, false);
       final notification notification = builderprogress.build();
       //发送一个通知
       manager.notify(no, notification);
       //创建一个计时器
       timer timer = new timer();
       timer.schedule(new timertask(){
         @override
         public void run() {
           log.i("progress",progress+"");
           while(progress <= ){
             progress ++;
             try {
               thread.sleep();
             } catch (interruptedexception e) {
               // todo auto-generated catch block
               e.printstacktrace();
             }
             //更新进度条
             builderprogress.setprogress(, progress, false);
             //再次通知
             manager.notify(no, builderprogress.build());
           }
           //计时器退出
           this.cancel();
           //进度条退出
           manager.cancel(no);
           return;//结束方法
         }
       }, );
     }
     break;
     default:
       break;
     }
   }
 }

5、运行:我的虚拟机版本是4.0的(api19),按住通知左(右)滑动就可以让通知小时了。

效果如下:

Android中通过Notification&NotificationManager实现消息通知