Android通知(Notification)使用的简单例子Demo

1.在Android软件中通知(Notification)是一个比较常见的特色功能,当某个应用不在运行当中时,可能会向用户发送一些信息,比如QQ、微信等应用中新消息提醒的通知栏通知等,本Demo中将展示其用法。

2.使用的开发工具为:Android Studio 3.1.1版本

a. 新建一个项目:Notification_Test,修改activity_main.xml中内容为:

<?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">
<Button
    android:id="@+id/send_notice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="发送通知"/>
</LinearLayout>

布局Design设计如下:


本Demo只是展示简单的使用例子,大家可以根据自己的需要修改后加入到自己的设计当中

紧接着完成MainActivity的设计:

在这里遇到一个小问题,就是当我们使用匿名内部类的监听方法时会报错:


网上查了一下(参考博客:https://blog.csdn.net/qq_34262794/article/details/78851134)之后可以改成使用接口的方式,就不会报错了,这个好像是android版本更新后的问题,修改如下后不报错:

package com.example.gahui.notification_test;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = (Button)findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
//        sendNotice.setOnClickListener(new View.OnClickListener() {//使用内部类监听方法会报错,这是Android系统版本的问题
//            @Override
//            public void onClick(View v) {
//                NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//                Notification notification = new Notification.Builder(this)
//                        .setContentTitle("标题")
//                        .setContentText("这里展示的是通知内容~")
//                        .setWhen(System.currentTimeMillis())
//                        .setSmallIcon(R.mipmap.ic_launcher)
//                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
//                        .build();
//                manager.notify(1,notification);
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.send_notice:
                Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
                NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this)//此处会有中间一道线,并不影响运行,这是android系统版本的问题
                        .setContentTitle("标题")  //显示通知的标题
                        .setContentText("这里展示的是通知内容~")//显示消息通知的内容
                        .setWhen(System.currentTimeMillis())//显示通知的具体时间
                        .setSmallIcon(R.mipmap.ic_launcher)//这里设置显示的是手机顶部系统通知的应用图标
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//这里设置显示的是下拉通知栏后显示的系统图标
                        .setContentIntent(pendingIntent)
                        //.setAutoCancel(true)//可以在此使用此方法,点击通知后,通知内容自动取消,也可以在NotificationActivity.java中设置方法取消显示通知内容
                        .setVibrate(new long[] {0,1000,1000,1000})//设置发出通知后震动一秒,停止一秒后再震动一秒,需要在manifest.xml中设置权限
                        .build();
                manager.notify(1,notification);
                break;
            default:
                break;
        }
    }
}

到这里,应用可以运行了,但是,我们使用过android智能手机都知道,我们可以在下拉状态栏点击通知后跳转到相应的的活动,那么怎么设置点击功能呢?请看下一步。

b. 之前如果对Intent有所了解的话,那么可能就会对点击事件的处理比较得心应手

首先新建一个活动使得点击之后可以跳转到对应的页面:

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你已点击通知来到此页面~"
        android:textSize="24dp"
        android:layout_margin="50dp"/>
</LinearLayout>

页面显示如下:


然后修改MainActivity的代码:

我这里还加入了发出通知后震动功能,更多的功能如添加声音等大家可以根据需要加入

switch (v.getId()){
    case R.id.send_notice:
        Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this)//此处会有中间一道线,并不影响运行,这是android系统版本的问题
                .setContentTitle("标题")  //显示通知的标题
                .setContentText("这里展示的是通知内容~")//显示消息通知的内容
                .setWhen(System.currentTimeMillis())//显示通知的具体时间
                .setSmallIcon(R.mipmap.ic_launcher)//这里设置显示的是手机顶部系统通知的应用图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//这里设置显示的是下拉通知栏后显示的系统图标
                .setContentIntent(pendingIntent)
                //.setAutoCancel(true)//可以在此使用此方法,点击通知后,通知内容自动取消,也可以在NotificationActivity.java中设置方法取消显示通知内容
                .setVibrate(new long[] {0,1000,1000,1000})//设置发出通知后震动一秒,停止一秒后再震动一秒,需要在manifest.xml中设置权限
                .build();
        manager.notify(1,notification);
        break;
    default:
        break;

成功完成Demo!

c. 结果演示:

额,由于本人还不会制作GIF图,之后记得的话会补上的~

代码经过在我本人手机上测试成功~

d. 完整源代码地址:https://download.csdn.net/download/qq_38442065/10388157

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android常驻通知(Notification)是指在用户状态栏中一直显示的通知图标和文本内容,不会因为用户操作或应用进程被销毁而消失。常驻通知通常用于实时监测、后台服务、音乐播放等需要持续提醒用户的场景。 常驻通知的实现步骤如下: 1. 首先,需要创建一个Notification对象,包括通知图标、标题、内容等信息。 2. 然后,创建一个PendingIntent,用于定义用户点击通知后的操作,比如打开应用的某个Activity或执行某个Service。 3. 创建一个NotificationChannel(通知渠道),用于定义通知的重要程度,包括声音、震动等设置。 4. 将Notification对象与PendingIntent关联,并将其设置为常驻通知的优先级。 5. 最后,调用NotificationManager的notify方法,将通知显示在用户的状态栏上。 需要注意的是,常驻通知存在一些使用限制和最佳实践: 1. 用户可以通过设置中的通知管理来关闭或打开特定应用的常驻通知。 2. 常驻通知不适合用于广告或频繁推送的内容,以免打扰用户。 3. 为了避免误导用户,常驻通知的图标和文本内容应与应用的实际情况相符。 4. 如果需要更新通知的内容或操作,可以使用NotificationManager的notify方法进行更新,并保持通知的id不变。 总之,常驻通知Android提供的一个重要功能,可以实现持续提醒用户和后台监测的需求。但应用开发者需要注意使用场景和用户体验,遵循Android的最佳实践,以确保用户对常驻通知的接受和理解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值