详解Android中Notification通知提醒

 更新时间:2016年01月24日 14:40:42   投稿:lijiao  
这篇文章主要为大家介绍了Android中Notification通知提醒,本文演示了普通的通知和自定义视图通知

在消息通知时,我们经常用到两个组件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通知提醒的文章,希望对大家的学习有所帮助。

相关文章

  • Kotlin函数式编程超详细介绍

    Kotlin函数式编程超详细介绍

    一个函数式应用通常由三大类函数构成:变换transform、过滤filters合并combineo每类函数都针对集合数据类型设计,目标是产生一个最终结果。函数式编程用到的函数生来都是可组合的,也就是说,你可以组合多个简单函数来构建复杂的计算行为
    2022-09-09
  • Android实现弹窗进度条效果

    Android实现弹窗进度条效果

    这篇文章主要为大家详细介绍了Android实现弹窗进度条效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android requestFocus详解及实例

    Android requestFocus详解及实例

    这篇文章主要介绍了Android requestFocus详解及实例的相关资料,需要的朋友可以参考下
    2017-07-07
  • Android Wear计时器开发

    Android Wear计时器开发

    这篇文章主要介绍了Android Wear计时器开发,需要的朋友可以参考下
    2014-11-11
  • Android中SoundPool的使用步骤实例

    Android中SoundPool的使用步骤实例

    今天小编就为大家分享一篇关于Android中SoundPool的使用步骤实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Android自定义HorizontalScrollView打造超强Gallery效果

    Android自定义HorizontalScrollView打造超强Gallery效果

    这篇文章主要介绍了Android自定义HorizontalScrollView打造图片横向滑动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Kotlin中协程的创建过程详析

    Kotlin中协程的创建过程详析

    使用协程的专业开发者中有超过 50% 的人反映使用协程提高了工作效率,下面这篇文章主要给大家介绍了关于Kotlin中协程创建过程的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-01-01
  • Android 捕获错误日志的方法

    Android 捕获错误日志的方法

    这篇文章主要介绍了Android 捕获错误日志的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Android打开WebView黑屏闪烁问题排查

    Android打开WebView黑屏闪烁问题排查

    这篇文章主要介绍了Android打开WebView黑屏闪烁问题排查,文章通过详细的代码示例和图文介绍WebView黑屏闪烁的问题,感兴趣的小伙伴可以跟着小编一起来学习
    2023-05-05
  • Android软键盘弹出时的界面控制方法

    Android软键盘弹出时的界面控制方法

    这篇文章主要介绍了Android软键盘弹出时的界面控制方法,结合实例形式分析了Android软键盘弹出后的三种模式,涉及Android针对AndroidManifet.xml的修改技巧,需要的朋友可以参考下
    2016-08-08

最新评论