zoukankan      html  css  js  c++  java
  • Android开发UI之Notification

    Notification,顾名思义,通知,就是我们常说的系统推送。

    官网链接:http://developer.android.com/reference/android/app/Notification.html

    要想使用Notification,还需要使用几个类,NotificationManagerNotificationCompat.BuilderPendingIntentIntent

    这个是官网教你如何创建一个Notificiation:http://developer.android.com/training/notify-user/build-notification.html#action

    根据官网的教程,练习创建一个Notification,并实现点击Notification跳转。

    1.使用NotificationCompat.Builder类,创建一个Notification Builder。

    NotificationCompat.Builder类位于support lib中的android.support.v4.app.NotificationCompat.Builder。继承自Object.

    1 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)   //使用的是support.v4.app.NotificationCompat.Builder
    2 mBuilder.setSmallIcon(R.drawable.notification_icon)                          //设置icon
    3 mBuilder.setContentTitle("My notification")                                  //设置标题Title
    4 mBuilder.setContentText("Hello World!");                                     //设置内容

    2.设置点击Notification的动作

    1 Intent resultIntent = new Intent(this, ResultActivity.class);                //使用Intent,设置跳转到ResultActivity
    2 PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); //使用PendingIntent
    3 mBuilder.setContentIntent(resultPendingIntent); //
    1 Notification notification=mBuilder.build();  //获取真实的nofitication

    3.发出通知

    1 int mNotificationId = 001;                                                   //设置Nofitication的ID
    2 NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //获取NotificationManager service实例
    3 mNotifyMgr.notify(mNotificationId,notification); //构建一个Notification,并发出

    以上就完成了一个Notication的显示和动作。

  • 相关阅读:
    双栈排序
    Koishi Loves Segments
    [USACO14FEB] Cow Decathlon 牛的十项全能
    [HNOI2016]网络
    koishi的数学题
    邦邦的大合唱站队
    #613(div2)
    Educational Codeforces Round 77
    #Hello 2020
    #601 (Div. 2)
  • 原文地址:https://www.cnblogs.com/liyiran/p/4655892.html
Copyright © 2011-2022 走看看