delphi安卓开发使用系统通知

在 Delphi 安卓开发中,系统通知是一个非常重要的功能,它可以让用户在使用应用程序时及时获得重要的提醒和信息。本文将会介绍 Delphi 安卓开发中使用系统通知的原理和详细操作流程。

一、系统通知的概念与分类

系统通知,就是在 Android 手机的状态栏上显示的提醒信息,包括通知标题、通知内容和通知图标等。通知一般包括两种类型:可清除和不可清除。可清除的通知是指通过用户的手动操作,可以清除掉该通知;而不可清除的通知则需要等到特定条件满足或被处理才能消除。

二、使用原理

通过 Delphi 调用 Android 系统 API,可以实现系统通知的功能。系统通知主要分为两部分:通知栏消息和后台服务。其中通知栏消息是负责在手机通知栏上显示提示信息,而后台服务则需要运行一个 Service 来维护通知的生命周期以及处理后续操作。

三、详细操作流程

以下是 Delphi 安卓开发中使用系统通知的详细操作流程:

1. 添加权限

首先需要在 AndroidManifest.xml 文件中添加以下权限:

```

//震动权限

//开机启动权限

//保持唤醒权限

//辅助功能权限

//通知权限

```

2. 创建 Service

创建一个 Service,用于定时检测并发送通知。代码如下:

```

unit MyService;

interface

uses

System.SysUtils, System.Classes, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes,Androidapi.JNI.Os,Androidapi.JNI.App,Androidapi.JNIBridge,Androidapi.JNI.NotificationManagerCompat,Androidapi.JNI.Notification,FMX.Platform.Android;

type

TMyService = class(TAndroidService)

procedure AndroidServiceCreate(Sender: TObject);

procedure AndroidServiceStartCommand(const Sender: TObject;

const Intent: JIntent; Flags, StartId: Integer; var StopService: Boolean);

private

{ Private 宣言 }

public

{ Public 宣言 }

end;

var

MyService: TMyService;

implementation

{%CLASSGROUP 'FMX.Controls.TControl'}

{$R *.dfm}

procedure TMyService.AndroidServiceCreate(Sender: TObject);

begin

// TODO: 繝味Λ繝��繝。繝シ繧ク繝」繝�繝ォ繧貞叙蠕励∈TAndroidService縺ョ譌・莠亥クコ螻樊€�

end;

procedure TMyService.AndroidServiceStartCommand(const Sender: TObject;

const Intent: JIntent; Flags, StartId: Integer; var StopService: Boolean);

var

NotificationManager: JNotificationManager;

Builder: JNotificationCompat_Builder;

Notification: JNotification;

Title, Content: JString;

begin

NotificationManager := TJNotificationManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE));

//创建Notification.Builder

Title := StringToJString('测试Notification');

Content := StringToJString('这个是一条测试Notification消息');

Builder := TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);

Builder.setSmallIcon(TAndroidHelper.Context.getApplicationInfo().icon);

Builder.setContentTitle(Title);

Builder.setContentText(Content);

Builder.setTicker(Content);

//设置意图

Builder.setContentIntent(nil);

Builder.setAutoCancel(True);

//发声或震动

Builder.setDefaults(TJNotification.JavaClass.DEFAULT_ALL);

//启动通知

Notification := Builder.build();

NotificationManager.notify(0, Notification);

//自毁

StopService := True;

end;

end.

```

3. 创建 BroadcastReceiver

创建一个 BroadcastReceiver 来接收系统通知的操作,代码如下:

```

unit NotificationClickReceiver;

interface

uses

System.SysUtils,System.Types,System.UITypes,System.Classes,Androidapi.JNI.GraphicsContentViewText,

Androidapi.JNI.JavaTypes,Androidapi.JNI.Os,Androidapi.Helpers,Androidapi.JNIBridge,Androidapi.JNI.App,

Androidapi.JNI.NotificationManagerCompat,Androidapi.JNI.Notification,FMX.Platform.Android,

System.Notification;

type

TNotificationClickReceiver = class(TJavaLocal, JFMXBroadcastReceiverListener)

public

procedure onReceive(Context: JContext; Intent: JIntent); cdecl;

end;

var

NotificationClickReceiver: TNotificationClickReceiver;

implementation

procedure TNotificationClickReceiver.onReceive(Context: JContext; Intent: JIntent);

var

NotificationManager: JNotificationManager;

Notification: JNotification;

begin

if not Assigned(NotificationManager) then

NotificationManager := TJNotificationManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE));

if not Assigned(Notification) then

Notification := TJNotification.Wrap((Intent.getParcelableExtra(TJNotification.JClass.EXTRA_NOTIFICATION) as IParcelable).GetObjectID);

NotificationManager.cancel(Notification.contentIntent.requestCode);

end;

end.

```

4. 为 Service 注册 BroadcastReceiver

在 Service 的 AndroidServiceCreate 方法中注册 BroadcastReceiver:

```

procedure TMyService.AndroidServiceCreate(Sender: TObject);

var

LReceiver: JFMXBroadcastReceiver;

begin

inherited;

LReceiver := TJFMXBroadcastReceiver.JavaClass.init;

LReceiver.setListener(NotificationClickReceiver);

LReceiver.addAction(TJNotificationManager.JavaClass.ACTION_NOTIFICATION_CLICKED);

TJLocalBroadcastManager.JavaClass.getInstance(TAndroidHelper.Context).registerReceiver(LReceiver, TJLocalBroadcastFilter.JavaClass.init(TJNotificationManager.JavaClass.ACTION_NOTIFICATION_CLICKED));

end;

```

5. 在主界面中启动 Service

在主界面代码中启动 Service:

```

uses

MyService;

procedure TForm1.Button1Click(Sender: TObject);

begin

TAndroidHelper.Context.StartService(TAndroidHelper.JavaServiceClass(TMyService));

end;

```

6. 发送系统通知

在 Service 中发送系统通知,代码如下:

```

NotificationManager := TJNotificationManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE));

//创建Notification.Builder

Title := StringToJString('测试Notification');

Content := StringToJString('这个是一条测试Notification消息');

Builder := TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);

Builder.setSmallIcon(TAndroidHelper.Context.getApplicationInfo().icon);

Builder.setContentTitle(Title);

Builder.setContentText(Content);

Builder.setTicker(Content);

//设置意图

Builder.setContentIntent(nil);

Builder.setAutoCancel(True);

//发声或震动

Builder.setDefaults(TJNotification.JavaClass.DEFAULT_ALL);

//启动通知

Notification := Builder.build();

NotificationManager.notify(0, Notification);

```

通过以上步骤,就可以在 Delphi 安卓应用中实现系统通知的功能了。这个功能可以给用户实时的消息推送和提示,可以让用户方便地了解当前应用程序的状态和提醒。


相关知识:
qt开发安卓软件教程
Qt是一套跨平台的C++图形用户界面应用程序开发框架,可以用于开发Windows、MacOS、Linux、Android和iOS等平台的应用程序。本文将简要介绍如何使用Qt开发Android应用程序。Qt开发Android应用程序的基本原理是使用Qt Cr
2023-05-23
nodejs 开发安卓app
Node.js 是一个非常强大的开发工具,适用于服务器端的 JavaScript 开发。在安卓 app 开发中,也可以使用 Node.js 来实现一些功能,比如后端接口的开发,数据库的连接等。下面是一些关于使用 Node.js 开发安卓 App 的原理和详
2023-05-23
eclipse安卓开发代码怎样调用图片
在Android应用程序中,经常需要向用户呈现图片来达到更好的视觉效果。当我们要在应用中使用多张图片时,如何在代码中调用这些图片呢?原理在Android中,所有的资源都是由资源管理器来管理的。这些资源包括图片、布局、字符串等等,都有其对应的资源ID。当我们
2023-05-23
delphi安卓原生开发
Delphi是一种高级编程语言,由Borland开发。与其他高级语言一样,Delphi具有面向对象的特点、高度模块化和可移植性,以及强大的GUI支持。除此之外,Delphi还可以用于开发移动应用程序,其中包括Android应用程序。下面将详细介绍如何使用D
2023-05-23
能不能用php开发安卓app
虽然 PHP 主要是一种用于 Web 开发的脚本语言,但通过某些技术和平台,它也可以用于开发 Android 应用程序。下面是使用 PHP 开发 Android 应用程序的原理和详细介绍:1. 使用 PhoneGap 或 Apache CordovaPho
2023-04-28
江苏安卓系统app定制开发报价方案
江苏安卓系统APP定制开发报价方案(原理或详细介绍)随着移动互联网的普及和智能手机的使用人数不断增加, 客户端应用程序(APPs)已经成为企业和个人推广产品和服务的重要工具。根据需求,定制开发一个符合用需求的安卓系统APP至关重要。在这篇文章中,我们将详细
2023-04-28
控件化安卓app开发
控件化是一种解耦的思想,它可以让 Android 应用程序在开发过程中更加灵活性,并增加了其可扩展性、可维护性以及可重用性。控件化开发可以让应用程序把各个功能模块分解成独立的控件,并独立地进行开发、测试、发布和使用。当一个应用程序需要新增某个功能时,只需要
2023-04-28
广州安卓商城app开发定制
广州安卓商城app开发定制涉及到以下几个方面的内容:1.功能设计:首先需要设计商城app的功能,如商品分类、购物车、支付、订单管理等。在设计功能的过程中,需要考虑商城的用户群体、商城的主营商品类型,以及是否考虑集成社交媒体等功能。2.UI/UX设计:商城a
2023-04-28
安卓app详细开发流程图
安卓APP开发流程图包括了从项目启动到最终发布的各个阶段。下面是一个详细的安卓APP开发流程图分析。1. 项目启动 a. 需求分析:分析用户需求,确定应用的目标、功能及交互设计。 b. 技术选型:根据需求分析,选择合适的技术栈,如开发工具、程序语言
2023-04-28
安卓 nas app 开发
### 安卓NAS App开发:原理与详细介绍网络附加存储(NAS,Network Attached Storage)是一种远程存储解决方案,允许用户通过网络访问存储设备,实现对数据的存储、备份和共享。对于安卓平台来说,开发一个NAS应用可以让用户通过手机
2023-04-28
上海手机安卓app开发
手机安卓app开发是一门需要开发者熟练掌握Java语言的技术。随着互联网的发展,手机成为人们日常生活中必不可少的工具,安卓app的开发也越来越受到关注。下面是一些原理和详细介绍。1. 安卓app的结构安卓app是一个由多个组件构成的应用程序。这些组件包括:
2023-04-28
app安卓制作
App制作是一个复杂的过程,需要涉及多个领域的技能。以下提供一些原理或详细介绍,帮助你了解如何制作Android应用程序。1. Java编程语言:Android应用程序主要使用Java编程语言,因此您需要了解Java语言及其基础知识,如变量、数据类型、操作
2023-04-28
©2015-2023 安卓益APP Anzhuoe.com 蜀ICP备17007734号-1