当前位置: 移动技术网 > IT编程>移动开发>Android > 详解Android中Notification通知提醒

详解Android中Notification通知提醒

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

荣海兰,香港搞笑鬼片大全,马鞍山采购

在消息通知时,我们经常用到两个组件toast和notification。特别是重要的和需要长时间显示的信息,用notification就最 合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,android这一创新性的ui组件赢得了用户的一 致好评,就连苹果也开始模仿了。今天我们就结合实例,探讨一下notification具体的使用方法。  首先说明一下我们需要实现的功能是:在程序启动时,发出一个通知,这个通知在软件运行过程中一直存在,相当于qq的托盘一样。

然后再演示一下普通的通知和自定义视图通知, 那我们就先建立一个安卓项目。

然后编辑/res/layout/main.xml文件,代码如下:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/black"
 android:orientation="vertical" >

 <textview
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:text="@string/title"
  android:textcolor="#0f0"
  android:textsize="20sp"
  android:textstyle="bold" />

 <button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginleft="50dp"
  android:layout_marginright="50dp"
  android:layout_margintop="30dp"
  android:onclick="normal"
  android:text="@string/notification"
  android:textcolor="#0f0"
  android:textsize="20sp" />

 <button
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginleft="50dp"
  android:layout_marginright="50dp"
  android:layout_margintop="30dp"
  android:onclick="custom"
  android:text="@string/custom"
  android:textcolor="#0f0"
  android:textsize="20sp" />

</linearlayout>

上面的布局很简单,有两个按钮分别用于启动普通的notification和自定义的notification。
接下来自定义一个布局用于显示自定义的通知的。

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#000"
 android:orientation="vertical" >

 <imageview
  android:id="@+id/iv"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:contentdescription="@string/action_settings"
  android:src="@drawable/ic_launcher" />

 <textview
  android:id="@+id/tv"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:textcolor="#0f0"
  android:textsize="15sp" />

</linearlayout>

接下来就是上代码。

package com.itfom.notification;

import android.app.activity;
import android.app.notification;
import android.app.notificationmanager;
import android.app.pendingintent;
import android.content.context;
import android.content.intent;
import android.net.uri;
import android.os.bundle;
import android.os.environment;
import android.view.view;
import android.widget.remoteviews;

public class mainactivity extends activity {

 private notificationmanager mnotificationmanager;
 private context context;
 private notification notification;
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 }

 //普通的通知
 @suppresswarnings("deprecation")
 public void normal(view v){
 //创建通知
 createnotification("普通的通知");
 //把通知放在正在运行栏目中
 notification.flags|=notification.flag_ongoing_event;
 //设定默认声音
 notification.defaults|=notification.default_sound;
 //设定默认震动
 notification.defaults|=notification.default_vibrate;
 //设定默认led灯提醒
 notification.defaults|=notification.default_lights;
 //设置点击后通知自动清除
 notification.defaults|=notification.flag_auto_cancel;

 string texttitle="notification示例";
 string textcontent="程序正在运行,点击此处跳转到演示界面";

 intent it=new intent(context, mainactivity.class);
 pendingintent pendintent=pendingintent.getactivity(context, 0, it, 0);
 notification.setlatesteventinfo(context, texttitle, textcontent, pendintent);
 mnotificationmanager.notify(0, notification);
 }

 //自定义的通知
 public void custom(view v){
 //创建通知
 createnotification("个性化的通知");
 //自定义通知的声音
 notification.sound=uri.parse(environment.getexternalstoragedirectory()+"/non.mp3");
 //自定义震动参数分别为多长时间开始震动、第一次震动的时间、停止震动的时间
 long[] vibrate={0,100,200,300};
 notification.vibrate=vibrate;
 //自定义闪光灯的方式
 notification.ledargb=0xff00ff00;
 notification.ledonms=500;
 notification.ledoffms=500;
 notification.flags|=notification.flag_show_lights;

 remoteviews contentview=new remoteviews(this.getpackagename(),r.layout.notify);
 contentview.settextviewtext(r.id.tv, "这是个性化的通知");
 //指定个性化的视图
 notification.contentview=contentview;
 intent it=new intent(context, mainactivity.class);
 pendingintent pendintent=pendingintent.getactivity(context, 0, it, 0);
 //指定内容视图
 notification.contentintent=pendintent;
 mnotificationmanager.notify(1, notification);
 }
 //自定义一个方法创建通知
 @suppresswarnings("deprecation")
 public notification createnotification(string text){
 context = this;
 mnotificationmanager=(notificationmanager) this.getsystemservice(context.notification_service);
 int icon=r.drawable.ic_launcher;
 long when=system.currenttimemillis();
 return notification = new notification(icon, text, when);
 }

 //重写onbackpressed事件
 @override
 public void onbackpressed() {
 super.onbackpressed();
 finish();
 //取消通知
 mnotificationmanager.cancel(0);
 android.os.process.killprocess(android.os.process.mypid());
 system.exit(0);
 }
}

上面的代码我们定义了一个方法createnotification(string text).该方法是用于创建一个通知。注意之所以这样写是因为。不管是普通的通知还是自定义的通知。前面的创建过程都是一样的。然后我们实现了两个按钮的点击事件。
运行结果如下所示:

 

当点击两个按钮时会出现以下的情况:

 

注 :这里是有声音效果的。如果手机支持闪光,还有led效果。

以上就是关于android中notification通知提醒实现的过程详解,最近更新了许多关于android中notification通知提醒的文章,希望对大家的学习有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网