Android之Notification的使用

前台服务

一个Service不管是被启动或是被绑定,默认是运行在后台的。有一种特殊的服务叫前台服务,是一种能被用户意识到它存在的服务,默认是不会被系统自动销毁的,但是必须提供一个状态栏Notification,在通知栏放置一个持续的标题。这个Notification 是不能被忽略的,除非服务被停止或从前台删除。这类服务主要用于一些需要用户能意识到它在后台运行并且随时可以操作的业务,如音乐播放器,设置为前台服务,使用一个Notification 显示在通知栏,可以使用户切歌或是暂停之类的。

前台服务与普通服务的定义规则是一样的,也需要继承 Service,这里没有区别,唯一的区别是在服务里需要使用 Service.startForeground(int id,Notification notification)方法设置当前服务为一个前台服务,并为其制定 Notification。其中的参数 id 是一个唯一标识通知的整数,但是这里注意这个整数一定不能为0,notification为前台服务的通知,并且这个 notification 对象只需要使用 startForeground()方法设置即可。前台服务可以通过调用 stopForeground(true)来使当前服务退出前台,但是并不会停止服务。

有一点需要注意,startForeground()需要在Android 2.0之后的版本才生效,在这之前的版本使用setForeground()来设置前台服务,并且需要NotificationManager 对象来管理通知,但是现在市面上基本上已经很少有2.0或以下的设备了,所以也不用太在意。

通过上面的介绍,会发现前台服务和 Notification 具有很强的关联,所以在讲解前台服务之前先对Noification进行简单的介绍,关于通知的更多内容,将在多媒体内容进行讲解。

 Notification 简介与使用

因为 Android 的快速发展(版本快速升级)出现了一些兼容性的问题。对于Notification 而言,Android 3.0 是一个分水岭,在其之前构建 Notification 推 荐使用NotificationCompate.Builder,是一个 Android向下版本的兼容包,而在 Android 3.0之后,一般推荐使用NotificationCompat.Builder方式构建。本文将使用 NotficationCompat.Builder方式构建 Notification。对于一个简单的通知,只需要设置下面几个属性即可∶

● 小图标,使用 setSmallIcon()方法设置。

● 标题,使用 setContentTitle()方法设置。

● 文本内容,使用 setContentText()方法设置。

当然,在使用通知时,一般情况下点击该通知能够执行指定的意图,这是使用 Pendinglntent 类来实现的,详细的内容也将在多媒体一章的 Notification 一节中讲述,这里只要能够看懂即可。

下面通过一个简单的实例让读者能够更直观地感受 Notification 是如何使用的。新建一个MainActivity,代码如下∶

package com.rfstar.notificationtest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        //高版本的模拟器或手机还需要开启渠道才能显示通知
        NotificationChannel notificationChannel=null;
        NotificationManager	notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            notificationChannel=new NotificationChannel("001","channel_name",NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder	builder=new NotificationCompat.Builder(MainActivity.this,"001");
        //实例化一个意图,当点击通知时会跳转执行这个意图
        Intent intent = new Intent(this, MainActivity.class);
        //将意图进行封装
        PendingIntent	pendingIntent= PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        //设置Notification的点击之后执行的意图
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("酷我音乐");
        builder.setContentText("正在播放的音乐:两只老虎");
        notification = builder.build();
        notificationManager.notify(0,notification);
   }
}

运行程序,打开通知栏,会出现一条通知,效果如下图所示。点击此通知将会跳转到MainActivity中

Android之Notification的使用_第1张图片

源码下载地址:链接:https://pan.baidu.com/s/1ExoSxaQRdjmwUUnDuSbUIQ 提取码:43ka

你可能感兴趣的:(android,studio,android,java,android-studio)