zoukankan      html  css  js  c++  java
  • Android Notification

    Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。

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

    状态栏和状态条

    状态条就是手机屏幕最上方的一个条形状的区域;

    在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;

    状态栏就是手从状态条滑下来的可以伸缩的view;

    在状态栏中一般有两类(使用FLAG_标记):

    (1)正在进行的程序

    (2)通知事件

    大概来描述创建一个Notification传送的信息有
    一个状态条图标
    在拉伸的状态栏窗口中显示带有大标题,小标题,图标的信息,并且有处理该点击事件:
    比如调用该程序的入口类
    闪光,LED,或者震动
    内容信息:

     

    实例化一个Notification.Builder对象;
    注意:在android:minSdkVersion <16以下时,可以使用
    NotificationCompat.Builder创建。

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical" >
     5 
     6     <Button
     7         android:layout_width="match_parent"
     8         android:layout_height="wrap_content"
     9         android:onClick="sendNotification"
    10         android:text="发送通知" />
    11 
    12     <Button
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:onClick="clearNotification"
    16         android:text="清除通知" />
    17 
    18     <Button
    19         android:layout_width="match_parent"
    20         android:layout_height="wrap_content"
    21         android:onClick="diyNotification"
    22         android:text="自定义通知" />
    23 
    24 </LinearLayout>
    activity_main
     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical">
     5 
     6     <TextView
     7         android:id="@+id/tv"
     8         android:layout_centerHorizontal="true"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:text="标题"/>
    12 
    13     <ImageView
    14         android:id="@+id/iv"
    15         android:layout_below="@id/tv"
    16         android:layout_centerHorizontal="true"
    17         android:layout_width="wrap_content"
    18         android:layout_height="wrap_content"
    19         android:src="@drawable/ic_launcher" />
    20 
    21 
    22 
    23 </RelativeLayout>
    notification_layout
     1 public class MainActivity extends Activity {
     2 
     3     public static final int CLICK_REQUEST = 1;
     4     public static final int DIY_CLICK_REQUEST = 2;
     5 
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.activity_main);
    10     }
    11 
    12     public void sendNotification(View v) {
    13         //minSdkVersion<16时,使用NotificationCompat.Builder创建
    14         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    15         //设置小图标
    16         builder.setSmallIcon(R.mipmap.ic_launcher);
    17         //标题
    18         builder.setContentTitle("大标题");
    19         //文本
    20         builder.setContentText("大新闻大新闻,这里显示大新闻");
    21         //发送的时间戳
    22         builder.setWhen(System.currentTimeMillis());
    23         //附加信息
    24         builder.setContentInfo("通知");
    25         //状态条中贴标内容
    26         builder.setTicker("新闻来了");
    27         //使用默认值DEFAULT_ALL,比如声音,震动,闪屏
    28         //DEFAULT_LIGHTS、DEFAULT_SOUNDS、DEFAULT_VIBRATE
    29         //加入手机震动,一定要在manifest.xml中加入权限
    30         // <uses-permission android:name="android.permission.VIBRATE" />
    31         builder.setDefaults(Notification.DEFAULT_ALL);
    32 
    33         //设置点击事件
    34         Intent intent = new Intent(this, MainActivity.class);
    35         PendingIntent pi = PendingIntent.getActivity(this, CLICK_REQUEST, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    36         builder.setContentIntent(pi);
    37 
    38         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    39         //创建一个Notification对象
    40         Notification notification = builder.build();
    41         //通知不能被状态栏的清除按钮清除掉
    42         notification.flags = Notification.FLAG_NO_CLEAR;
    43         //标记为id的通知
    44         manager.notify((int) (Math.random() * 100), notification);
    45     }
    46 
    47     public void clearNotification(View v) {
    48         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    49         manager.cancelAll();
    50     }
    51 
    52     //自定义布局Notification
    53     public void diyNotification(View v) {
    54         RemoteViews view =  new RemoteViews(getPackageName(), R.layout.notification_layout);
    55         Notification notification = new NotificationCompat.Builder(this)
    56                 .setSmallIcon(R.mipmap.ic_launcher)
    57                 .setContent(view)
    58                 .setContentTitle("自定义通知")
    59                 .setContentText("自定义通知内容")
    60                 .setTicker("自定义通知来了")
    61                 .build();
    62         notification.flags = Notification.FLAG_NO_CLEAR;
    63         view.setTextColor(R.id.tv, Color.BLUE);
    64 
    65         //点击图片打开MainActivity
    66         Intent intent = new Intent(MainActivity.this, MainActivity.class);
    67         PendingIntent pi_iv = PendingIntent.getActivity(this,DIY_CLICK_REQUEST,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    68         view.setOnClickPendingIntent(R.id.iv,pi_iv);
    69 
    70         //点击"跳转界面"打开OneActivity
    71         Intent intent2 = new Intent(MainActivity.this, OneActivity.class);
    72         PendingIntent pi_tv = PendingIntent.getActivity(this,DIY_CLICK_REQUEST,intent2,PendingIntent.FLAG_UPDATE_CURRENT);
    73         view.setOnClickPendingIntent(R.id.tv,pi_tv);
    74 
    75         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    76         manager.notify(1,notification);
    77 
    78     }
    79 }
    MainActivity.java
     1 public class OneActivity extends Activity {
     2 
     3     @Override
     4     protected void onCreate(@Nullable Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         TextView tv = new TextView(this);
     7         tv.setText("界面跳转");
     8         setContentView(tv);
     9     }
    10 
    11 }
    OneActivity.java

    别忘了注册OneActivity

  • 相关阅读:
    【安全测试】sqlmap安装方法
    【安全测试】 WebScarab安装方法
    【安全测试】burpsuite安装方法
    【Jenkins】Windows下安装&访问jenkins
    【Python】下拉框元素的找法
    【python】selenium+python自动化测试环境搭建
    【接口测试】Jenkins+Ant+Jmeter搭建持续集成的接口测试平台
    【修改端口号】linux下修改apache,nginx服务端口号
    【linux】linux下yum安装后Apache、php、mysql默认安装路径
    地址
  • 原文地址:https://www.cnblogs.com/Claire6649/p/5971113.html
Copyright © 2011-2022 走看看