当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程自定义Notification实例分析

Android编程自定义Notification实例分析

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

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

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

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

❑创建一个状态条图标。

❑在扩展的状态条窗口中显示额外的信息(和启动一个intent)。

❑闪灯或led。

❑电话震动。

❑发出听得见的警告声(铃声,保存的声音文件)。

自定义notification效果图:

 

自定义的布局文件:

<?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:

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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网