服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - 详解Android中Notification的使用方法

详解Android中Notification的使用方法

2021-04-25 15:03cjjky Android

这篇文章主要介绍了Android中Notification的使用方法,最典型的应用就是未看短信和未接来电的显示,还有QQ微信,想要深入了解Notification的朋友可以参考本文

      在消息通知的时候,我们经常用到两个控件notification和toast。特别是重要的和需要长时间显示的信息,用notification最合适不过了。他可以在顶部显示一个图标以标示有了新的通知,当我们拉下通知栏的时候,可以看到详细的通知内容。
      最典型的应用就是未看短信和未接来电的显示,还有qq微信,我们一看就知道有一个未接来电或者未看短信,收到qq离线信息。同样,我们也可以自定义一个notification来定义我们自己的程序想要传达的信息。

notification我把他分为两种,一种是默认的显示方式,另一种是自定义的,今天为大家讲述默认的显示方式
1、程序框架结构图如下

详解Android中Notification的使用方法

2、布局文件 main.xml 源码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<textview 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:textcolor="#eee"
 android:textstyle="bold"
 android:textsize="25sp"
 android:text="notificationdemo实例" />
<button
 android:id="@+id/btnsend"
 android:text="send notification"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"/> 
</linearlayout>

3、mainactivity.java源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.andyidea.notification;
 
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.view.view;
import android.widget.button;
 
public class mainactivity extends activity {
 private button btnsend;
  
 //定义broadcastreceiver的action
 private static final string notificationdemo_action = "com.andyidea.notification.notificationdemo_action";
  
 /** called when the activity is first created. */
 @override
 public void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.main);
   
  btnsend = (button)findviewbyid(r.id.btnsend);
  btnsend.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    intent intent = new intent();
    intent.setaction(notificationdemo_action);
    sendbroadcast(intent);
   }
  });
 }
  
}

4、布局文件 secondlayou.xml 源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<linearlayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <textview 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:textcolor="#eee"
 android:textstyle="bold"
 android:textsize="25sp"
 android:text="显示通知界面" />
<button
 android:id="@+id/btncancel"
 android:text="cancel notification"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center" /> 
</linearlayout>

5、secondactivity.java源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.andyidea.notification;
 
import android.app.activity;
import android.app.notification;
import android.app.notificationmanager;
import android.app.pendingintent;
import android.content.intent;
import android.os.bundle;
import android.view.view;
import android.widget.button;
 
public class secondactivity extends activity {
 
 private button btncancel;
 //声明notification
 private notification notification;
 //声明notificationmanager
 private notificationmanager mnotification;
 //标识notification的id
 private static final int id = 1;
  
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.secondlayout);
   
  btncancel = (button)findviewbyid(r.id.btncancel);
  //怎样获得notificationmanager的实例?
  string service = notification_service;
  mnotification = (notificationmanager)getsystemservice(service);
   
  //获得notification的实例
  notification = new notification();
   
  //设置该图标 会在状态栏显示
  int icon = notification.icon = android.r.drawable.stat_sys_phone_call;
  //设置提示信息
  string tickertext = "test notification";
  //设置显示时间
  long when = system.currenttimemillis();
  notification.icon = icon;
  notification.tickertext = tickertext;
  notification.when = when;
   
  intent intent = new intent(this, mainactivity.class);
  pendingintent pi = pendingintent.getactivity(this, 0, intent, 0);
  notification.setlatesteventinfo(this, "消息", "sms android", pi);
  mnotification.notify(id, notification);
   
  btncancel.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    mnotification.cancel(id); //--->取消通知
   }
  });
 }
  
}

6、notificationreceiver.java源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.andyidea.notification;
 
import com.andyidea.notification.secondactivity;
 
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
 
public class notificationreceiver extends broadcastreceiver {
 
 @override
 public void onreceive(context context, intent intent) {
  //实例化intent
  intent i = new intent();
  //在新任务中启动activity
  i.setflags(intent.flag_activity_new_task);
  //设置intent启动的组件名称
  i.setclass(context, secondactivity.class);
  //启动activity,显示通知
  context.startactivity(i);
 }
 
}

7、程序运行效果如下:

详解Android中Notification的使用方法

以上就是针对android中notification使用方法进行的详细介绍,希望对大家的学习有所启发,帮助大家更好地学习android软件编程。

延伸 · 阅读

精彩推荐