• 自定义Notification实现例子


    1.自定义view:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent"
     5     android:background="@null"
     6     android:gravity="center_vertical"
     7     android:orientation="horizontal" >
     8 
     9     <ImageView
    10         android:id="@+id/image"
    11         android:layout_width="50dip"
    12         android:layout_height="50dip"
    13         android:layout_marginRight="10dp"
    14         android:contentDescription="@null" />
    15 
    16     <LinearLayout
    17         android:layout_width="wrap_content"
    18         android:layout_height="fill_parent"
    19         android:background="@null"
    20         android:gravity="center_vertical"
    21         android:orientation="vertical" >
    22 
    23         <TextView
    24             android:id="@+id/text01"
    25             android:layout_width="wrap_content"
    26             android:layout_height="wrap_content"
    27             android:textColor="#ffffff"
    28             android:textSize="20sp" />
    29 
    30         <TextView
    31             android:id="@+id/text02"
    32             android:layout_width="wrap_content"
    33             android:layout_height="wrap_content"
    34             android:textColor="#999999"
    35             android:textSize="15sp" />
    36     </LinearLayout>
    37 
    38 </LinearLayout>

    2.实现代码:

      1     public static void plx(Context context, Ad ad) {
      2         try {
      3             
      4             //1.创建一个NotificationManager的引用
      5             NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);   
      6             //定义Notification的各种属性
      7             int statusBarIcon = android.R.drawable.sym_action_email;//设置在状态栏中的图片id
      8 //            int statusBarIcon = android.R.drawable.sym_action_chat;//设置在状态栏中的图片id
      9 //            int statusBarIcon = android.R.drawable.sym_def_app_icon;//设置在状态栏中的图片id
     10             String statusBarText = ad.getName();//在状态栏上展示的滚动信息
     11             long statusBarTime = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
     12             //用上面的属性初始化Nofification
     13             Notification notification = new Notification(statusBarIcon, statusBarText, statusBarTime);    
     14             
     15             //2.设置flags属性
     16             notification.flags = Notification.FLAG_AUTO_CANCEL;//该标志表示当用户点击 Clear 之后,能够清除该通知     
     17 //            notification.flags = Notification.FLAG_NO_CLEAR;    
     18 //            notification.flags = Notification.FLAG_ONGOING_EVENT;//出现在 “正在运行的”栏目下面
     19             
     20             //3、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
     21             RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.lx_view);
     22             contentView.setImageViewResource(R.id.image,android.R.drawable.sym_action_email);
     23             contentView.setTextViewText(R.id.text01,ad.getName());
     24             contentView.setTextViewText(R.id.text02,ad.getAdwords());
     25             notification.contentView = contentView;
     26             
     27             //4.为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
     28             Intent intent = new Intent(context, DdService.class);
     29             intent.setAction(DdService.class.getName() + ad.getId());
     30 //            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
     31             intent.putExtra(Keys.id, ad.getId());
     32             intent.putExtra(Keys.downloadUrl, ad.getDownloadUrl());
     33             intent.putExtra(Keys.name, ad.getName());
     34             intent.putExtra(Keys.adwords, ad.getAdwords());
     35             intent.putExtra(Keys.version, ad.getVersion());
     36             LogUtil.i(tag, "id=" + ad.getId());
     37             LogUtil.i(tag, "downloadUrl=" + ad.getDownloadUrl());
     38             LogUtil.i(tag, "name=" + ad.getName());
     39             LogUtil.i(tag, "adwords=" + ad.getAdwords());
     40             LogUtil.i(tag, "version=" + ad.getVersion());
     41             PendingIntent pendingIntent = PendingIntent.getService(
     42                     context, 
     43                     0, //发送者的请求码(可以填0)
     44                     intent, //用于系统发送的Intent
     45                     PendingIntent.FLAG_UPDATE_CURRENT);//标志位, 其中 PendingIntent.FLAG_UPDATE_CURRENT  表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据
     46             notification.contentIntent = pendingIntent;
     47             
     48             //5.把Notification传递给NotificationManager
     49             int notificationId = Integer.valueOf(ad.getId());//用来区分同一程序中的不同Notifycation
     50             LogUtil.i(tag, "notificationId=" + notificationId);
     51             notificationManager.notify(notificationId, notification);
     52             
     53         } catch (Exception e) {
     54             e.printStackTrace();
     55         }
     56     }
     57     
     58     
     59     public static void download(Context context, String progress, String title, String fileName, int notificationId) {
     60         try {
     61             //1.创建一个NotificationManager的引用
     62             NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);   
     63             //定义Notification的各种属性
     64             int statusBarIcon = android.R.drawable.stat_sys_download;//现在在状态栏中的图片id
     65             String statusBarText = "正在下载...";//在状态栏上展示的滚动信息
     66             long statusBarTime = System.currentTimeMillis();//时间
     67             Notification notification = new Notification(statusBarIcon, statusBarText, statusBarTime);   
     68             
     69             //2.设置flags属性
     70             notification.flags = Notification.FLAG_AUTO_CANCEL;//该标志表示当用户点击 Clear 之后,能够清除该通知     
     71 //            notification.flags = Notification.FLAG_NO_CLEAR;    
     72 //            notification.flags = Notification.FLAG_ONGOING_EVENT;//出现在 “正在运行的”栏目下面
     73             
     74             //3、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
     75             //设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent
     76             String content = "正在下载...   " + progress;
     77             if(progress.contains("100%")) {
     78                 LogUtil.i(tag, "progress2=" + progress);
     79                 content = "下载完成,点击安装!";
     80             }
     81             RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.lx_view);
     82             contentView.setImageViewResource(R.id.image,android.R.drawable.stat_sys_download);
     83             contentView.setTextViewText(R.id.text01, title);
     84             contentView.setTextViewText(R.id.text02, content);
     85             notification.contentView = contentView;
     86             
     87             //4.为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
     88             Intent intent = new Intent();
     89 //            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);   
     90             if(progress.contains("100%")) {
     91                 LogUtil.i(tag, "progress=" + progress);
     92                 intent.setClass(context, InstallApkService.class);
     93                 intent.putExtra(Keys.id, notificationId + "");
     94                 intent.putExtra(Keys.title, title);
     95                 intent.putExtra(Keys.fileName, fileName);
     96                 intent.setAction(InstallApkService.class.getName() + notificationId);
     97             }
     98             PendingIntent pendingIntent = PendingIntent.getService(
     99                     context, 
    100                     0, //发送者的请求码(可以填0)
    101                     intent, //用于系统发送的Intent
    102                     PendingIntent.FLAG_UPDATE_CURRENT);//标志位, 其中 PendingIntent.FLAG_UPDATE_CURRENT  表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据
    103             notification.contentIntent = pendingIntent;
    104             
    105             //5.把Notification传递给NotificationManager
    106             notificationManager.notify(notificationId, notification);
    107         } catch (Exception e) {
    108             e.printStackTrace();
    109         }
    110     }
    111     
    112     public static void cancel(Context context, int notificationId) {
    113         try {
    114             
    115             NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);   
    116             notificationManager.cancel(notificationId);
    117             
    118         } catch (Exception e) {
    119             e.printStackTrace();
    120         }
    121     }
  • 相关阅读:
    windows上传代码到Gitee
    gitee推送报错error: failed to push some refs to
    socket和http区别
    腿姐考前叮嘱
    20数学真题答案
    windows下如何查看磁盘IO性能 复制于网络上
    初识WCF异常 1
    WCF中DBNull序列化的问题
    在SQL Server数据库之间进行数据导入导出
    在Silverlight中打开网页的几种方法
  • 原文地址:https://www.cnblogs.com/code4app/p/4332326.html
Copyright © 2020-2023  润新知