zoukankan      html  css  js  c++  java
  • Android之Notification的多种用法

     

    [置顶] Android之Notification的多种用法

    标签: notification
     分类:
     

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

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

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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package net.loonggg.notification;  
    2.   
    3. import android.app.Activity;  
    4. import android.app.Notification;  
    5. import android.app.NotificationManager;  
    6. import android.app.PendingIntent;  
    7. import android.content.Context;  
    8. import android.content.Intent;  
    9. import android.os.Bundle;  
    10. import android.view.View;  
    11. import android.widget.RemoteViews;  
    12.   
    13. public class MainActivity extends Activity {  
    14.     private static final int NOTIFICATION_FLAG = 1;  
    15.   
    16.     @Override  
    17.     protected void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         setContentView(R.layout.activity_main);  
    20.     }  
    21.   
    22.     public void notificationMethod(View view) {  
    23.         // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。  
    24.         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
    25.         switch (view.getId()) {  
    26.         // 默认通知  
    27.         case R.id.btn1:  
    28.             // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity  
    29.             PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,  
    30.                     new Intent(this, MainActivity.class), 0);  
    31.             // 下面需兼容Android 2.x版本是的处理方式  
    32.             // Notification notify1 = new Notification(R.drawable.message,  
    33.             // "TickerText:" + "您有新短消息,请注意查收!", System.currentTimeMillis());  
    34.             Notification notify1 = new Notification();  
    35.             notify1.icon = R.drawable.message;  
    36.             notify1.tickerText = "TickerText:您有新短消息,请注意查收!";  
    37.             notify1.when = System.currentTimeMillis();  
    38.             notify1.setLatestEventInfo(this, "Notification Title",  
    39.                     "This is the notification message", pendingIntent);  
    40.             notify1.number = 1;  
    41.             notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。  
    42.             // 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示  
    43.             manager.notify(NOTIFICATION_FLAG, notify1);  
    44.             break;  
    45.         // 默认通知 API11及之后可用  
    46.         case R.id.btn2:  
    47.             PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,  
    48.                     new Intent(this, MainActivity.class), 0);  
    49.             // 通过Notification.Builder来创建通知,注意API Level  
    50.             // API11之后才支持  
    51.             Notification notify2 = new Notification.Builder(this)  
    52.                     .setSmallIcon(R.drawable.message) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap  
    53.                                                         // icon)  
    54.                     .setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在status  
    55.                                                                 // bar上显示的提示文字  
    56.                     .setContentTitle("Notification Title")// 设置在下拉status  
    57.                                                             // bar后Activity,本例子中的NotififyMessage的TextView中显示的标题  
    58.                     .setContentText("This is the notification message")// TextView中显示的详细内容  
    59.                     .setContentIntent(pendingIntent2) // 关联PendingIntent  
    60.                     .setNumber(1) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。  
    61.                     .getNotification(); // 需要注意build()是在API level  
    62.             // 16及之后增加的,在API11中可以使用getNotificatin()来代替  
    63.             notify2.flags |= Notification.FLAG_AUTO_CANCEL;  
    64.             manager.notify(NOTIFICATION_FLAG, notify2);  
    65.             break;  
    66.         // 默认通知 API16及之后可用  
    67.         case R.id.btn3:  
    68.             PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,  
    69.                     new Intent(this, MainActivity.class), 0);  
    70.             // 通过Notification.Builder来创建通知,注意API Level  
    71.             // API16之后才支持  
    72.             Notification notify3 = new Notification.Builder(this)  
    73.                     .setSmallIcon(R.drawable.message)  
    74.                     .setTicker("TickerText:" + "您有新短消息,请注意查收!")  
    75.                     .setContentTitle("Notification Title")  
    76.                     .setContentText("This is the notification message")  
    77.                     .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API  
    78.                                                                             // level16及之后增加的,API11可以使用getNotificatin()来替代  
    79.             notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。  
    80.             manager.notify(NOTIFICATION_FLAG, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示  
    81.             break;  
    82.         // 自定义通知  
    83.         case R.id.btn4:  
    84.             // Notification myNotify = new Notification(R.drawable.message,  
    85.             // "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());  
    86.             Notification myNotify = new Notification();  
    87.             myNotify.icon = R.drawable.message;  
    88.             myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";  
    89.             myNotify.when = System.currentTimeMillis();  
    90.             myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除  
    91.             RemoteViews rv = new RemoteViews(getPackageName(),  
    92.                     R.layout.my_notification);  
    93.             rv.setTextViewText(R.id.text_content, "hello wrold!");  
    94.             myNotify.contentView = rv;  
    95.             Intent intent = new Intent(Intent.ACTION_MAIN);  
    96.             PendingIntent contentIntent = PendingIntent.getActivity(this, 1,  
    97.                     intent, 1);  
    98.             myNotify.contentIntent = contentIntent;  
    99.             manager.notify(NOTIFICATION_FLAG, myNotify);  
    100.             break;  
    101.         case R.id.btn5:  
    102.             // 清除id为NOTIFICATION_FLAG的通知  
    103.             manager.cancel(NOTIFICATION_FLAG);  
    104.             // 清除所有的通知  
    105.             // manager.cancelAll();  
    106.             break;  
    107.         default:  
    108.             break;  
    109.         }  
    110.     }  
    111. }  
    再看主布局文件:
    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:tools="http://schemas.android.com/tools"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:orientation="vertical"  
    6.     tools:context=".MainActivity" >  
    7.   
    8.     <Button  
    9.         android:id="@+id/btn1"  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="wrap_content"  
    12.         android:onClick="notificationMethod"  
    13.         android:text="默认通知(已被抛弃,但是通用)" />  
    14.   
    15.     <Button  
    16.         android:id="@+id/btn2"  
    17.         android:layout_width="fill_parent"  
    18.         android:layout_height="wrap_content"  
    19.         android:onClick="notificationMethod"  
    20.         android:text="默认通知(API11之后可用)" />  
    21.   
    22.     <Button  
    23.         android:id="@+id/btn3"  
    24.         android:layout_width="fill_parent"  
    25.         android:layout_height="wrap_content"  
    26.         android:onClick="notificationMethod"  
    27.         android:text="默认通知(API16之后可用)" />  
    28.   
    29.     <Button  
    30.         android:id="@+id/btn4"  
    31.         android:layout_width="fill_parent"  
    32.         android:layout_height="wrap_content"  
    33.         android:onClick="notificationMethod"  
    34.         android:text="自定义通知" />  
    35.   
    36.     <Button  
    37.         android:id="@+id/btn5"  
    38.         android:layout_width="fill_parent"  
    39.         android:layout_height="wrap_content"  
    40.         android:onClick="notificationMethod"  
    41.         android:text="清除通知" />  
    42.   
    43. </LinearLayout>  
    还有一个是:自定义通知的布局文件my_notification.xml,代码如下:
    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="wrap_content"  
    5.     android:background="#ffffff"  
    6.     android:orientation="vertical" >  
    7.   
    8.     <TextView  
    9.         android:id="@+id/text_content"  
    10.         android:layout_width="wrap_content"  
    11.         android:layout_height="wrap_content"  
    12.         android:textSize="20sp" />  
    13.   
    14. </LinearLayout>  
    转载请注明出处:http://blog.csdn.net/loongggdroid/article/details/17616509
  • 相关阅读:
    快速导出B站收藏单节目列表
    打包Python程序
    PHP应用日志记录
    这几天折腾win10访问deepin共享的历程
    PHP更新本地公网IP到阿里云域名解析,实现DDNS
    PHP.ini 能不能加载子配置文件 ?
    PHP处理表单数据的一个安全回顾(记录教训)
    不用U盘,用一台好电脑给另一个电脑重装windows10
    方正 ignb路由器设置备份(自用笔记)
    Xiuno BBS 4.0 修改时间显示
  • 原文地址:https://www.cnblogs.com/developer-ios/p/5560620.html
Copyright © 2011-2022 走看看