当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] 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不显示。

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