当前位置:首页 > 技术 > 正文内容

Android 勿扰模式默认应用开发指南

访客 技术 2026年7月8日 2

构建 Android 勿扰模式管理应用

在 Android 平台上,通过系统级通知策略控制功能,可实现对勿扰模式(Do Not Disturb, DND)的自定义管理。本教程将引导你开发一个能够切换系统勿扰状态的应用,适用于需要精准控制通知干扰的场景。

开发流程概览

步骤说明
1初始化项目并配置基础结构
2声明并请求通知策略访问权限
3设计用户交互界面用于状态查看与操作
4集成 NotificationManager 实现模式切换逻辑
5完成测试与权限引导机制

详细实现步骤

1. 创建项目

使用 Android Studio 新建一个空白活动项目,命名为 DNDController,选择最低支持 API 级别为 23(Android 6.0)以确保兼容性。

2. 添加必要权限

AndroidManifest.xml 中声明以下权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dndcontroller">

    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

    <application
        android:allowBackup="true"
        android:label="DND 控制器"
        ...>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

该权限允许应用读取和修改系统的通知中断策略,是启用核心功能的前提。

3. 设计用户界面

编辑 res/layout/activity_main.xml 文件,添加状态文本和控制按钮:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="24dp">

    <TextView
        android:id="@+id/statusLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="当前状态:关闭"
        android:textSize="18sp"
        android:layout_gravity="center"
        android:layout_marginBottom="32dp" />

    <Button
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切换勿扰模式"
        android:layout_gravity="center" />

</LinearLayout>

4. 实现核心逻辑

MainActivity.java 中实现状态切换与权限检查:

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private NotificationManager notificationManager;
    private TextView statusDisplay;
    private Button toggleButton;
    private boolean isDNDActive = false;

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

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        statusDisplay = findViewById(R.id.statusLabel);
        toggleButton = findViewById(R.id.toggleButton);

        toggleButton.setOnClickListener(v -> handleDNDToggle());

        updateDisplay();
    }

    private void handleDNDToggle() {
        if (isDNDActive) {
            // 关闭勿扰模式:恢复所有通知
            notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
            isDNDActive = false;
        } else {
            // 启用勿扰模式:屏蔽所有通知
            if (notificationManager.isNotificationPolicyAccessGranted()) {
                notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
                isDNDActive = true;
            } else {
                // 权限未授予,跳转至设置页面
                Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                startActivity(intent);
                return;
            }
        }
        updateDisplay();
    }

    private void updateDisplay() {
        String message = isDNDActive ? "当前状态:开启" : "当前状态:关闭";
        statusDisplay.setText(message);
    }
}

关键点说明:

  • INTERRUPTION_FILTER_NONE:完全屏蔽通知,即进入勿扰模式。
  • INTERRUPTION_FILTER_ALL:允许所有通知通过,即退出勿扰模式。
  • isNotificationPolicyAccessGranted():用于判断是否已获得系统级权限。
  • 若无权限,应引导用户手动授权,否则无法生效。

5. 测试与调试建议

部署到真实设备上进行验证。首次运行时需打开系统设置中的"通知策略"开关,授予应用权限。可通过发送测试通知来确认模式切换是否正常生效。

附加说明

此应用仅作为系统功能扩展,不替代原生设置。其核心价值在于提供一键式控制入口,尤其适合希望快速启用或禁用勿扰模式的用户。

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。