Android中Notification的作用是什么

发布时间:2021-06-26 15:25:18 作者:Leah
来源:亿速云 阅读:193

今天就跟大家聊聊有关Android中Notification的作用是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Android的系统有一个通知栏,这个通知栏很管用。通知是Android系统跟用户交互的一个重要的部分。比如有新邮件,新短信息,未接来电等等信息都会显示在通知栏。我们把通知栏拖下来,点击通知信息,就能显示通知的具体内容。很cool的功能。

我们也可以定义通知的样式,有图标,通知的声音,还有LED的颜色,闪烁的频率等等。

我们看一下例子

 1: package com.halzhang.android.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.Intent;   8: import android.os.Bundle;   9: import android.view.View;  10: import android.widget.Button;  11: /**  12:  * 通知  13:  * @author 张汉国  14:  */  15: public class NotificationDemo extends Activity {  16:     /** Called when the activity is first created. */  17:     //通知id  18:     private static final int NOTICE_ID = 1222;  19:     private Button notify;  20:     private Button cancel;  21:     @Override  22:     public void onCreate(Bundle savedInstanceState) {  23:         super.onCreate(savedInstanceState);  24:         setContentView(R.layout.main);  25:         notify = (Button) findViewById(R.id.noti);  26:         cancel = (Button) findViewById(R.id.cancel);  27:         notify.setOnClickListener(new View.OnClickListener() {  28:               29:             @Override  30:             public void onClick(View v) {  31:                 notityMe();  32:             }  33:         });  34:           35:         cancel.setOnClickListener(new View.OnClickListener() {  36:               37:             @Override  38:             public void onClick(View v) {  39:                 final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  40:                 //取消通知  41:                 manager.cancel(NOTICE_ID);  42:             }  43:         });  44:     }  45:       46:     private void notityMe(){  47:         //获得通知管理器,通知是一项系统服务  48:         final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  49:         //初始化通知对象 p1:通知的图标 p2:通知的状态栏显示的提示 p3:通知显示的时间  50:         Notification notification = new Notification(R.drawable.icon, "通知测试", System.currentTimeMillis());  51:         //点击通知后的Intent,此例子点击后还是在当前界面  52:         PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationDemo.class), 0);  53:         //设置通知信息  54:         notification.setLatestEventInfo(this, "通知title", "通知信息内容", intent);  55:         //通知  56:         manager.notify(NOTICE_ID, notification);  57:     }  58: }

看完上述内容,你们对Android中Notification的作用是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. 【Android】监听Notification被清除
  2. Android 学习笔记--android——Notification

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android notification

上一篇:layer中Icon样式的示例分析

下一篇:ES6中字符串怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》