zoukankan      html  css  js  c++  java
  • Notification状态栏通知

    Notification分为普通通知和自定义的两种,由图标、ticker(提示信息)、标题、内容、时间、事件组成,

    普通通知demo:

     1 package com.android.hzynotification;
     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 
    12 public class MainActivity extends Activity {
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         setContentView(R.layout.activity_main);
    18     }
    19 
    20     public void send(View v){
    21         // 1.得到通知管理器
    22         NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    23         // 2.构建通知
    24         Notification notification = new Notification(android.R.drawable.stat_notify_chat, "这是提示信息", System.currentTimeMillis());
    25         // 3.设置通知点击事件
    26         Intent intent = new Intent(this,MainActivity.class);
    27         PendingIntent contentIntent = PendingIntent.getActivity(this, 100, intent, 0);
    28         notification.setLatestEventInfo(this, "通知的标题", "通知的内容", contentIntent);
    29         
    30         notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击通知后自动消失
    31         
    32         // 4.发送通知
    33         nm.notify(100, notification);
    34     }
    35 }

    自定义通知的布局设置:

     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="match_parent"
     5     android:orientation="horizontal" >
     6     
     7     <ImageView 
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:src="@drawable/ic_launcher"
    11         />
    12     <LinearLayout 
    13         android:layout_marginLeft="3dp"
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:orientation="vertical"
    17         >
    18         <TextView 
    19             android:layout_marginTop="2dp"
    20             android:layout_width="wrap_content"
    21             android:layout_height="wrap_content"
    22             android:text="自定义的通知,专业拦截器"
    23             />
    24         <ProgressBar 
    25             android:layout_marginTop="2dp"
    26             android:layout_width="match_parent"
    27             android:layout_height="wrap_content"
    28             style="@android:style/Widget.ProgressBar.Horizontal"
    29             />
    30     </LinearLayout>
    31     
    32 
    33 </LinearLayout>

    自定义通知代码如下:

      

     1 package com.android.hzynotification;
     2 import android.app.Activity;
     3 import android.app.Notification;
     4 import android.app.NotificationManager;
     5 import android.app.PendingIntent;
     6 import android.content.Context;
     7 import android.content.Intent;
     8 import android.os.Bundle;
     9 import android.view.View;
    10 import android.widget.RemoteViews;
    11 
    12 public class MainActivity extends Activity {
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         setContentView(R.layout.activity_main);
    18     }
    19 
    20     
    21     public void costom(View v){
    22         // 1.得到通知管理器
    23         NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    24         // 2.构建通知
    25         Notification notification = new Notification();
    26         notification.icon = android.R.drawable.stat_notify_missed_call; // 图标
    27         notification.tickerText = "有未接电话哦";
    28         
    29         RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.content);
    30         notification.contentView = contentView; // 设置通知显示的布局
    31         
    32         // 设置通知点击的事件
    33         Intent intent = new Intent(this,OtherActivity.class);
    34         PendingIntent contentIntent = PendingIntent.getActivity(this, 100, intent, 0);
    35         notification.contentIntent = contentIntent;
    36         
    37         // 设置点击通知后自动消失
    38         notification.flags = Notification.FLAG_AUTO_CANCEL;
    39         // 发出通知
    40         nm.notify(100, notification);
    41         
    42     }
    43 }

  • 相关阅读:
    Zend Guard 7 , Zend Guard Loader处理PHP加密
    [转]pHP源码加密方法调查
    使用USB Key(加密狗)实现身份认证
    4*4行列式矩阵键盘
    HDU-2546-饭卡
    HDU-2073-无限的路
    HDU-2065-"红色病毒"问题
    HDU-2063-过山车
    HDU-2060-Snooker
    HDU-2056-Rectangles
  • 原文地址:https://www.cnblogs.com/androidez/p/2893563.html
Copyright © 2011-2022 走看看