服务器之家

服务器之家 > 正文

Android编程自定义Notification实例分析

时间:2021-04-27 15:44     来源/作者:傅荣康

本文实例讲述了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程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部