温馨提示  2024年 6月我们已经停止开发者板块文章内容更新,谢谢来访。存档数据
知识 2024-01-10 32 次阅读

Delphi安卓开发如何使用系统通知?有哪些疑问?

Delphi安卓开发中使用系统通知是一项实用的功能,可以向用户传达重要的信息或提醒。通过使用Delphi,开发者可以轻松地创建自定义的通知,并在安卓设备上显示它们。系统通知可以用于各种目的,如更新应用程序的状态、提醒用户进行操作或提供重要警告。使用Delphi安卓开发中的系统通知,开发者可以确保通知的外观和感觉符合应用程序的整体风格,并提供易于理解的视觉和听觉提示。通过合理使用系统通知,Delphi开发者能够给用户提供更优质、更直观的体验。

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

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

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

二、使用原理

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

三、详细操作流程

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

1. 添加权限

首先需要在 安卓Manifest.xml 文件中添加以下权限

```

//震动权限

//开机启动权限

//保持唤醒权限

//辅助功能权限

//通知权限

```

2. 创建 Service

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

```

unit MyService;

interface

uses

System.SysUtils, System.Classes, 安卓api.JNI.GraphicsContentViewText, 安卓api.JNI.JavaTypes,安卓api.JNI.Os,安卓api.JNI.App,安卓api.JNIBridge,安卓api.JNI.NotificationManagerCompat,安卓api.JNI.Notification,FMX.Platform.安卓;

type

TMyService = class(T安卓Service)

procedure 安卓ServiceCreate(Sender: TObject);

procedure 安卓ServiceStartCommand(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.安卓ServiceCreate(Sender: TObject);

begin

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

end;

procedure TMyService.安卓ServiceStartCommand(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(T安卓Helper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION,SERVICE));

//创建Notification.Builder

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

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

Builder := TJNotificationCompat,Builder.JavaClass.init(T安卓Helper.Context);

Builder.setSmallIcon(T安卓Helper.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,安卓api.JNI.GraphicsContentViewText,

安卓api.JNI.JavaTypes,安卓api.JNI.Os,安卓api.Helpers,安卓api.JNIBridge,安卓api.JNI.App,

安卓api.JNI.NotificationManagerCompat,安卓api.JNI.Notification,FMX.Platform.安卓,

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(T安卓Helper.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 的 安卓ServiceCreate 方法中注册 BroadcastReceiver

```

procedure TMyService.安卓ServiceCreate(Sender: TObject);

var

LReceiver: JFMXBroadcastReceiver;

begin

inherited;

LReceiver := TJFMXBroadcastReceiver.JavaClass.init;

LReceiver.setListener(NotificationClickReceiver);

LReceiver.addAction(TJNotificationManager.JavaClass.ACTION,NOTIFICATION,CLICKED);

TJLocalBroadcastManager.JavaClass.getInstance(T安卓Helper.Context).registerReceiver(LReceiver, TJLocalBroadcastFilter.JavaClass.init(TJNotificationManager.JavaClass.ACTION,NOTIFICATION,CLICKED));

end;

```

5. 在主界面中启动 Service

在主界面代码中启动 Service

```

uses

MyService;

procedure TForm1.Button1Click(Sender: TObject);

begin

T安卓Helper.Context.StartService(T安卓Helper.JavaServiceClass(TMyService));

end;

```

6. 发送系统通知

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

```

NotificationManager := TJNotificationManager.Wrap(T安卓Helper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION,SERVICE));

//创建Notification.Builder

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

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

Builder := TJNotificationCompat,Builder.JavaClass.init(T安卓Helper.Context);

Builder.setSmallIcon(T安卓Helper.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 安卓应用中实现系统通知的功能了。这个功能可以给用户实时的消息推送和提示,能够让用户方便地了解当前应用程序的状态和提醒。