全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android Notification的多种用法总结

Android Notification的多种用法总结

我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。

       我们也知道Android系统也是在不断升级的,有关Notification的用法也就有很多种,有的方法已经被android抛弃了,现在我实现了三种不同的方法,并适应不同的android版本。现在我就把代码公布出来,我喜欢把解释写在代码中,在这里我就不多说了,先看效果图:

再看代码,主要的代码如下:

package net.loonggg.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; 
 
public class MainActivity extends Activity { 
  private static final int NOTIFICATION_FLAG = 1; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
  } 
 
  public void notificationMethod(View view) { 
    // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。 
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    switch (view.getId()) { 
    // 默认通知 
    case R.id.btn1: 
      // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
          new Intent(this, MainActivity.class), 0); 
      // 下面需兼容Android 2.x版本是的处理方式 
      // Notification notify1 = new Notification(R.drawable.message, 
      // "TickerText:" + "您有新短消息,请注意查收!", System.currentTimeMillis()); 
      Notification notify1 = new Notification(); 
      notify1.icon = R.drawable.message; 
      notify1.tickerText = "TickerText:您有新短消息,请注意查收!"; 
      notify1.when = System.currentTimeMillis(); 
      notify1.setLatestEventInfo(this, "Notification Title", 
          "This is the notification message", pendingIntent); 
      notify1.number = 1; 
      notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。 
      // 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示 
      manager.notify(NOTIFICATION_FLAG, notify1); 
      break; 
    // 默认通知 API11及之后可用 
    case R.id.btn2: 
      PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, 
          new Intent(this, MainActivity.class), 0); 
      // 通过Notification.Builder来创建通知,注意API Level 
      // API11之后才支持 
      Notification notify2 = new Notification.Builder(this) 
          .setSmallIcon(R.drawable.message) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap 
                            // icon) 
          .setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在status 
                                // bar上显示的提示文字 
          .setContentTitle("Notification Title")// 设置在下拉status 
                              // bar后Activity,本例子中的NotififyMessage的TextView中显示的标题 
          .setContentText("This is the notification message")// TextView中显示的详细内容 
          .setContentIntent(pendingIntent2) // 关联PendingIntent 
          .setNumber(1) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。 
          .getNotification(); // 需要注意build()是在API level 
      // 16及之后增加的,在API11中可以使用getNotificatin()来代替 
      notify2.flags |= Notification.FLAG_AUTO_CANCEL; 
      manager.notify(NOTIFICATION_FLAG, notify2); 
      break; 
    // 默认通知 API16及之后可用 
    case R.id.btn3: 
      PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0, 
          new Intent(this, MainActivity.class), 0); 
      // 通过Notification.Builder来创建通知,注意API Level 
      // API16之后才支持 
      Notification notify3 = new Notification.Builder(this) 
          .setSmallIcon(R.drawable.message) 
          .setTicker("TickerText:" + "您有新短消息,请注意查收!") 
          .setContentTitle("Notification Title") 
          .setContentText("This is the notification message") 
          .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API 
                                      // level16及之后增加的,API11可以使用getNotificatin()来替代 
      notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。 
      manager.notify(NOTIFICATION_FLAG, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示 
      break; 
    // 自定义通知 
    case R.id.btn4: 
      // Notification myNotify = new Notification(R.drawable.message, 
      // "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis()); 
      Notification myNotify = new Notification(); 
      myNotify.icon = R.drawable.message; 
      myNotify.tickerText = "TickerText:您有新短消息,请注意查收!"; 
      myNotify.when = System.currentTimeMillis(); 
      myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除 
      RemoteViews rv = new RemoteViews(getPackageName(), 
          R.layout.my_notification); 
      rv.setTextViewText(R.id.text_content, "hello wrold!"); 
      myNotify.contentView = rv; 
      Intent intent = new Intent(Intent.ACTION_MAIN); 
      PendingIntent contentIntent = PendingIntent.getActivity(this, 1, 
          intent, 1); 
      myNotify.contentIntent = contentIntent; 
      manager.notify(NOTIFICATION_FLAG, myNotify); 
      break; 
    case R.id.btn5: 
      // 清除id为NOTIFICATION_FLAG的通知 
      manager.cancel(NOTIFICATION_FLAG); 
      // 清除所有的通知 
      // manager.cancelAll(); 
      break; 
    default: 
      break; 
    } 
  } 
} 

再看主布局文件:

<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" 
  tools:context=".MainActivity" > 
 
  <Button 
    android:id="@+id/btn1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="notificationMethod" 
    android:text="默认通知(已被抛弃,但是通用)" /> 
 
  <Button 
    android:id="@+id/btn2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="notificationMethod" 
    android:text="默认通知(API11之后可用)" /> 
 
  <Button 
    android:id="@+id/btn3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="notificationMethod" 
    android:text="默认通知(API16之后可用)" /> 
 
  <Button 
    android:id="@+id/btn4" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="notificationMethod" 
    android:text="自定义通知" /> 
 
  <Button 
    android:id="@+id/btn5" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="notificationMethod" 
    android:text="清除通知" /> 
 
</LinearLayout> 

还有一个是:自定义通知的布局文件my_notification.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="wrap_content" 
  android:background="#ffffff" 
  android:orientation="vertical" > 
 
  <TextView 
    android:id="@+id/text_content" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" /> 
 
</LinearLayout> 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


# Android  # Notification  # Notification用法  # Android Notification 使用方法详解  # Android编程使用Service实现Notification定时发送功能示例  # Android 通知使用权(NotificationListenerService)的使用  # Android Notification使用方法详解  # Android 使用AlarmManager和NotificationManager来实现闹钟和通知  # android使用NotificationListenerService监听通知栏消息  # Android实现Service下载文件  # Notification显示下载进度的示例  # Android Notification使用方法总结  # 请注意  # 自定义  # 是在  # 短消息  # 可以使用  # 的是  # 管理器  # 多个  # 将被  # 再看  # 后才  # 需要注意  # 栏中  # 我就  # 来了  # 你是  # 也就  # 在这  # 说了  # 在那里 


相关文章: 如何用搬瓦工VPS快速搭建个人网站?  清除minerd进程的简单方法  php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】  西安大型网站制作公司,西安招聘网站最好的是哪个?  网站制作价目表怎么做,珍爱网婚介费用多少?  c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】  如何选择服务器才能高效搭建专属网站?  已有域名如何免费搭建网站?  哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?  网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?  如何在万网主机上快速搭建网站?  建站之星如何快速更换网站模板?  招贴海报怎么做,什么是海报招贴?  已有域名和空间如何快速搭建网站?  网站专业制作公司有哪些,做一个公司网站要多少钱?  行程制作网站有哪些,第三方机票电子行程单怎么开?  建站VPS推荐:2025年高性能服务器配置指南  网站好制作吗知乎,网站开发好学吗?有什么技巧?  如何在云指建站中生成FTP站点?  建站之星如何优化SEO以实现高效排名?  如何通过网站建站时间优化SEO与用户体验?  如何在Golang中处理模块冲突_解决依赖版本不兼容问题  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  如何零基础在云服务器搭建WordPress站点?  建站主机数据库如何配置才能提升网站性能?  如何快速选择适合个人网站的云服务器配置?  如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?  网站建设制作需要多少钱费用,自己做一个网站要多少钱,模板一般多少钱?  制作企业网站建设方案,怎样建设一个公司网站?  公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?  建站之星上传入口如何快速找到?  5种Android数据存储方式汇总  建站主机默认首页配置指南:核心功能与访问路径优化  如何高效利用亚马逊云主机搭建企业网站?  大型企业网站制作流程,做网站需要注册公司吗?  如何通过宝塔面板实现本地网站访问?  如何通过虚拟机搭建网站?详细步骤解析  TestNG的testng.xml配置文件怎么写  建站之星后台管理系统如何操作?  宝塔建站后网页无法访问如何解决?  网站制作哪家好,cc、.co、.cm哪个域名更适合做网站?  单页制作网站有哪些,朋友给我发了一个单页网站,我应该怎么修改才能把他变成自己的呢,请求高手指点迷津?  如何确保西部建站助手FTP传输的安全性?  建站之星安装后如何配置SEO及设计样式?  如何打造高效商业网站?建站目的决定转化率  网站制作费用多少钱,一个网站的运营,需要哪些费用?  php8.4新语法match怎么用_php8.4match表达式替代switch【方法】  微网站制作教程,不会写代码,不会编程,怎么样建自己的网站?  如何彻底卸载建站之星软件?  常州自助建站工具推荐:低成本搭建与模板选择技巧 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。