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

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

服务器之家 - 编程语言 - Android - Android编程自定义Notification实例分析

Android编程自定义Notification实例分析

2021-04-27 15:44傅荣康 Android

这篇文章主要介绍了Android编程自定义Notification的用法,结合实例形式简单分析了自定义Notification的具体功能与实现技巧,需要的朋友可以参考下

本文实例讲述了android编程自定义notification的用法。分享给大家供大家参考,具体如下:

notification是一种让你的应用程序在不使用activity的情况下警示用户,notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径。

作为ui部分,notification对移动设备来说是最适合不过的了。用户可能随时都带着手机在身边。一般来说,用户会在后台打开几个程序,但不会注意它们。在这样的情形下,当发生需要注意的事件时,能够通知用户是很重要的。

notification由notificationmanger统一管理,目前包含的能力有:

  • 创建一个状态条图标。
  • 在扩展的状态条窗口中显示额外的信息(和启动一个intent)。
  • 闪灯或led。
  • 电话震动。
  • 发出听得见的警告声(铃声,保存的声音文件)。

自定义notification效果图:

Android编程自定义Notification实例分析 Android编程自定义Notification实例分析

自定义的布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<linearlayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <textview
 android:id="@+id/tv_rv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="haha"
 />
<progressbar
 style="@android:style/widget.progressbar.horizontal"
 android:id="@+id/pb_rv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 />
</linearlayout>

创建notification:

?
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
public class customnotificationactivity extends activity {
  notificationmanager notificationmanager;
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    //获取到系统的notificationmanager
    notificationmanager = (notificationmanager) getsystemservice(context.notification_service);
  }
  public void click(view view ){
    //实例化一个notification
     string tickertext = "ip号码 设置完毕";
     long when = system.currenttimemillis();
     notification notification = new notification(r.drawable.icon, tickertext, when);
     //不能手动清理
     //notification.flags= notification.flag_no_clear;
     //添加音乐
     //notification.sound = uri.parse("/sdcard/haha.mp3");
     //设置用户点击notification的动作
     // pendingintent 延期的意图
     intent intent = new intent(this,bactivity.class);
     pendingintent pendingintent = pendingintent.getactivity(this, 0, intent, 0);
     notification.contentintent = pendingintent;
     //自定义界面
     remoteviews rv = new remoteviews(getpackagename(), r.layout.noti_layout);
     rv.settextviewtext(r.id.tv_rv, "我是自定义的 notification");
     rv.setprogressbar(r.id.pb_rv, 80, 20, false);
     notification.contentview = rv;
     //把定义的notification 传递给 notificationmanager
     notificationmanager.notify(0, notification);
  }
}

希望本文所述对大家android程序设计有所帮助。

延伸 · 阅读

精彩推荐