当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] Notification是显示在手机状态的通知,一般显示手机当前的网络状态、电池状态、时间等。设置Notification涉及到两个类,一个类是NotificationManager,一个类是Notificatio

 Notification是显示在手机状态的通知,一般显示手机当前的网络状态、电池状态、时间等。

设置Notification涉及到两个类,一个类是NotificationManager,一个类是Notification。可以这样理解这两个类,NotificationManager相当于顺丰快递小哥,notification代表的就是我们送的信件,我们要发送信件首先打电话给顺丰小哥,相当于初始化NotificationManager,然后填写好信件,相当于初始化Notifiaction,然后小哥发送信件,就是notificationmanager.notify(notifaction)

1)初始化NotificationManager: this.getSystemService(Context.NOTIFICATION_SERVICE);

2)初始化Notification:new Notification.Builder(this).builder();

main.xml:

01 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
[!--empirenews.page--]
02     xmlns:tools="http://schemas.android.com/tools"
[!--empirenews.page--]
03     android:layout_width="match_parent"
[!--empirenews.page--]
04     android:layout_height="match_parent"
[!--empirenews.page--]
05     tools:context=".Main" >
[!--empirenews.page--]
06  
07     <Button
[!--empirenews.page--]
08         android:id="@+id/btn"
[!--empirenews.page--]
09         android:layout_width="wrap_content"
[!--empirenews.page--]
10         android:layout_height="wrap_content"
[!--empirenews.page--]
11         android:text="发送notification" />
[!--empirenews.page--]
12  
13     <Button
[!--empirenews.page--]
14         android:id="@+id/deletebtn"
[!--empirenews.page--]
15         android:layout_width="wrap_content"
[!--empirenews.page--]
16         android:layout_height="wrap_content"
[!--empirenews.page--]
17         android:layout_marginLeft="200sp"
[!--empirenews.page--]
18         android:text="取消notification" />
[!--empirenews.page--]
19  
20 </RelativeLayout>
[!--empirenews.page--]

Main.java:

01 package com.app.main;
[!--empirenews.page--]
02  
03 import android.annotation.SuppressLint;
[!--empirenews.page--]
04 import android.app.Activity;
[!--empirenews.page--]
05 import android.app.Notification;
[!--empirenews.page--]
06 import android.app.NotificationManager;
[!--empirenews.page--]
07 import android.app.PendingIntent;
[!--empirenews.page--]
08 import android.content.Context;
[!--empirenews.page--]
09 import android.content.Intent;
[!--empirenews.page--]
10 import android.os.Bundle;
[!--empirenews.page--]
11 import android.view.View;
[!--empirenews.page--]
12 import android.view.View.OnClickListener;
[!--empirenews.page--]
13 import android.widget.Button;
[!--empirenews.page--]
14 import android.widget.Toast;
[!--empirenews.page--]
15  
16 public class Main extends Activity {
[!--empirenews.page--]
17  
18     NotificationManager manager = null;
[!--empirenews.page--]
19  
20     Button button, btn1;
[!--empirenews.page--]
21  
22     int responseCode = 0x123;
[!--empirenews.page--]
23  
24     @SuppressLint("NewApi")
[!--empirenews.page--]
25     @Override
[!--empirenews.page--]
26     protected void onCreate(Bundle savedInstanceState) {
[!--empirenews.page--]
27  
28         super.onCreate(savedInstanceState);
[!--empirenews.page--]
29  
30         setContentView(R.layout.main);
[!--empirenews.page--]
31  
32         button = (Button) this.findViewById(R.id.btn);
[!--empirenews.page--]
33  
34         btn1 = (Button) this.findViewById(R.id.deletebtn);
[!--empirenews.page--]
35  
36         manager = (NotificationManager) this
[!--empirenews.page--]
37                 .getSystemService(Context.NOTIFICATION_SERVICE);
[!--empirenews.page--]
38  
39         button.setOnClickListener(new OnClickListener() {
[!--empirenews.page--]
40  
41             @Override
[!--empirenews.page--]
42             public void onClick(View view) {
[!--empirenews.page--]
43  
44                 send();
[!--empirenews.page--]
45  
46             }
[!--empirenews.page--]
47  
48         });
[!--empirenews.page--]
49  
50         btn1.setOnClickListener(new OnClickListener() {
[!--empirenews.page--]
51  
52             @Override
[!--empirenews.page--]
53             public void onClick(View view) {
[!--empirenews.page--]
54  
55                 deleteMsg();
[!--empirenews.page--]
56             }
[!--empirenews.page--]
57  
58         });
[!--empirenews.page--]
59  
60     }
[!--empirenews.page--]
61  
62     /**
[!--empirenews.page--]
63      *
[!--empirenews.page--]
64      */
[!--empirenews.page--]
65     @SuppressLint("NewApi")
[!--empirenews.page--]
66     public void send() {
[!--empirenews.page--]
67  
68         Intent intent = new Intent(Main.this, OtherActivity.class);
[!--empirenews.page--]
69  
70         PendingIntent pi = PendingIntent.getActivity(this0, intent, 0);
[!--empirenews.page--]
71  
72         Notification notification = new Notification.Builder(this)
[!--empirenews.page--]
73                 .setAutoCancel(true).setTicker("aaaa")
[!--empirenews.page--]
74                 .setSmallIcon(R.drawable.ic_launcher).setContentTitle("bbbb")
[!--empirenews.page--]
75                 .setContentText("ccccc")
[!--empirenews.page--]
76                 .setDefaults(Notification.DEFAULT_VIBRATE).build();
[!--empirenews.page--]
77  
78         manager.notify(responseCode, notification);
[!--empirenews.page--]
79  
80     }
[!--empirenews.page--]
81  
82     public void deleteMsg() {
[!--empirenews.page--]
83         manager.cancel(responseCode);
[!--empirenews.page--]
84     }
85 }
[!--empirenews.page--]

实现效果:

tips:在实例化Notification.Builder()实例的时候,必须setSmallIcon()或者setLargerIcon(),否则notifaction不显示。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

舍弗勒以"专注驱动技术的科技公司"为主题亮相IAA MOBILITY 2025(B3馆B40展台) 合并纬湃科技后首次亮相IAA MOBILITY,展示拓展后的汽车产品组合 凭借在软件、...

关键字: 电气 软件 驱动技术 BSP

香港2025年 9月12日 /美通社/ -- 全球领先的互联网社区创建者 - 网龙网络控股有限公司 ("网龙"或"本公司",香港交易所股票代码:777)欣然宣布,其子公司My...

关键字: AI 远程控制 控制技术 BSP

深圳2025年9月11日 /美通社/ -- 2025 年 9 月 10 日,第 26 届中国国际光电博览会(简称 "CIOE 中国光博会")在深圳盛大开幕。本届展会吸引力再创新高,全球超3800家优质...

关键字: 自动化 光电 CIO BSP

天津2025年9月11日 /美通社/ -- 国际能源署(IEA)数据显示,2024 年全球数据中心电力消耗达 415 太瓦时,占全球总用电量的 1.5%,预计到 2030 年,这一数字将飙升至 945 太瓦时,近乎翻番,...

关键字: 模型 AI 数据中心 BSP

北京2025年9月11日 /美通社/ -- 国际9月11日上午,2025年中国国际服务贸易交易会(以下简称"服贸会")—体育赛事经济高质量发展大会现场,北京经济技术开发区工委委员、管委会副主...

关键字: 5G BSP GROUP MOTOR

柏林2025年9月9日 /美通社/ -- 2025年9月5日,纳斯达克上市公司优克联集团(NASDAQ: UCL)旗下全球互联品牌GlocalMe,正式亮相柏林国际消费电子展(IFA 2025),重磅推出融合企...

关键字: LOCAL LM BSP 移动网络

深圳2025年9月9日 /美通社/ -- PART 01活动背景 当技术的锋芒刺穿行业壁垒,万物互联的生态正重塑产业疆域。2025年,物联网产业迈入 "破界创造"与"共生进化" 的裂变时代——AI大模型消融感知边界,...

关键字: BSP 模型 微信 AIOT

"出海无界 商机无限"助力企业构建全球竞争力 深圳2025年9月9日 /美通社/ -- 2025年8月28日, 由领先商业管理媒体世界经理人携手环球资源联合主办、深圳•前海出海e站通协办的...

关键字: 解码 供应链 AI BSP

柏林2025年9月9日 /美通社/ -- 柏林当地时间9月6日,在2025德国柏林国际电子消费品展览会(International Funkausstellung...

关键字: 扫地机器人 耳机 PEN BSP

武汉2025年9月9日 /美通社/ -- 7月24日,2025慧聪跨业品牌巡展——湖北•武汉站在武汉中南花园酒店隆重举办!本次巡展由慧聪安防网、慧聪物联网、慧聪音响灯光网、慧聪LED屏网、慧聪教育网联合主办,吸引了安防、...

关键字: AI 希捷 BSP 平板
关闭