【发布时间】:2014-09-15 13:15:52
【问题描述】:
unit Main;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.ListView.Types, IPPeerClient, System.Rtti, System.Bindings.Outputs,
Fmx.Bind.Editors, Data.Bind.EngExt, Fmx.Bind.DBEngExt, Data.Bind.Components,
REST.Client, Data.Bind.ObjectScope, FMX.StdCtrls, FMX.ListView, System.JSON, REST.types,
FMX.Layouts, FMX.Memo, FMX.ListBox, REST.Json, FMX.Colors, FMX.Objects, gcmnotification,
FMX.Edit;
type
TForm1 = class(TForm)
ToolBar1: TToolBar;
ToolBar2: TToolBar;
Label1: TLabel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;
BindingsList1: TBindingsList;
MemoContent: TMemo;
LinkControlToFieldContent: TLinkControlToField;
panel2: TPanel;
ToolBar3: TToolBar;
Label2: TLabel;
btnExit: TSpeedButton;
Label3: TLabel;
email: TEdit;
Label4: TLabel;
Rectangle1: TRectangle;
btnReg: TButton;
popup: TRectangle;
Label5: TLabel;
btnOK: TColorButton;
Label6: TLabel;
procedure SpeedButton1Click(Sender: TObject);
procedure Label5Click(Sender: TObject);
procedure btnExitClick(Sender: TObject);
procedure btnRegClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
gcmn: TGCMNotification;
end;
const
GCM_SENDERID = '460004329921';
API_ID = 'AIzaSyBclEqDL-yFJRZZ6ESpBZLACJ27ROh7oao';
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.btnExitClick(Sender: TObject);
begin
FreeAndNil(Application);
end;
procedure TForm1.btnRegClick(Sender: TObject);
begin
gcmn.SENDERID := GCM_SENDERID;
if gcmn.doRegister then
begin
if email.Text='' then
popup.Visible := true
else
begin
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.AddParameter('type', 'register', TRESTRequestParameterKind.pkGETorPOST);
RESTRequest1.AddParameter('regID', gcmn.RegistrationID, TRESTRequestParameterKind.pkGETorPOST);
RESTRequest1.AddParameter('email', email.Text, TRESTRequestParameterKind.pkGETorPOST);
RESTRequest1.Execute;
end;
end;
end;
procedure TForm1.Label5Click(Sender: TObject);
begin
popup.Visible:=false;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
jValue:TJSONValue;
begin
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.AddParameter('type', 'select', TRESTRequestParameterKind.pkGETorPOST);
RESTRequest1.AddParameter('table_name', 'person', TRESTRequestParameterKind.pkGETorPOST);
RESTRequest1.Execute;
jValue:=RESTResponse1.JSONValue;
MemoContent.Text := jValue.ToString;
end;
end.
当我尝试运行程序时,它会将应用程序安装到模拟器,但应用程序没有启动。
我有一个错误:
第一次机会例外,价格为 408839F8。异常类分割 故障 (11)。处理 mClient.apk (1795)
unit gcmnotification;
interface
{$IFDEF ANDROID}
uses
System.SysUtils,
System.Classes,
FMX.Helpers.Android,
Androidapi.Helpers,
Androidapi.JNI.PlayServices,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes;
type
TGCMNotificationMessageKind = (nmMESSAGE_TYPE_MESSAGE, nmMESSAGE_TYPE_DELETED, nmMESSAGE_TYPE_SEND_ERROR);
{ Discription of notification for Notification Center }
TGCMNotificationMessage = class (TPersistent)
private
FKind: TGCMNotificationMessageKind;
FSender: string;
FWhat: integer;
FBody: string;
protected
procedure AssignTo(Dest: TPersistent); override;
public
{ Unique identificator for determenation notification in Notification list }
property Kind: TGCMNotificationMessageKind read FKind write FKind;
property Sender: string read FSender write FSender;
property What: integer read FWhat write FWhat;
property Body: string read FBody write FBody;
constructor Create;
end;
TOnReceiveGCMNotification = procedure (Sender: TObject; ANotification: TGCMNotificationMessage) of object;
TGCMNotification = class(TComponent)
strict private
{ Private declarations }
FRegistrationID: string;
FSenderID: string;
FOnReceiveGCMNotification: TOnReceiveGCMNotification;
FReceiver: JBroadcastReceiver;
FAlreadyRegistered: boolean;
function CheckPlayServicesSupport: boolean;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function DoRegister: boolean;
function GetGCMInstance: JGoogleCloudMessaging;
published
{ Published declarations }
property SenderID: string read FSenderID write FSenderID;
property RegistrationID: string read FRegistrationID write FRegistrationID;
property OnReceiveGCMNotification: TOnReceiveGCMNotification read FOnReceiveGCMNotification write FOnReceiveGCMNotification;
end;
{$ENDIF}
implementation
{$IFDEF ANDROID}
uses
uGCMReceiver;
{ TGCMNotification }
function TGCMNotification.CheckPlayServicesSupport: boolean;
var
resultCode: integer;
begin
resultCode := TJGooglePlayServicesUtil.JavaClass.isGooglePlayServicesAvailable(SharedActivity);
result := (resultCode = TJConnectionResult.JavaClass.SUCCESS);
end;
constructor TGCMNotification.Create(AOwner: TComponent);
var
Filter: JIntentFilter;
begin
inherited;
Filter := TJIntentFilter.Create;
FReceiver := TJGCMReceiver.Create(Self);
SharedActivity.registerReceiver(FReceiver, Filter);
FAlreadyRegistered := false;
end;
destructor TGCMNotification.Destroy;
begin
SharedActivity.unregisterReceiver(FReceiver);
FReceiver := nil;
inherited;
end;
function TGCMNotification.DoRegister: boolean;
var
p: TJavaObjectArray<JString>;
gcm: JGoogleCloudMessaging;
begin
if FAlreadyRegistered then
result := true
else
begin
if CheckPlayServicesSupport then
begin
gcm := GetGCMInstance;
p := TJavaObjectArray<JString>.Create(1);
p.Items[0] := StringToJString(FSenderID);
FRegistrationID := JStringToString(gcm.register(p));
FAlreadyRegistered := (FRegistrationID <> '');
result := FAlreadyRegistered;
end
else
result := false;
end;
end;
function TGCMNotification.GetGCMInstance: JGoogleCloudMessaging;
begin
result := TJGoogleCloudMessaging.JavaClass.getInstance(SharedActivity.getApplicationContext);
end;
{ TGCMNotificationMessage }
procedure TGCMNotificationMessage.AssignTo(Dest: TPersistent);
var
DestNotification: TGCMNotificationMessage;
begin
if Dest is TGCMNotificationMessage then
begin
DestNotification := Dest as TGCMNotificationMessage;
DestNotification.Kind := Kind;
DestNotification.What := What;
DestNotification.Sender := Sender;
DestNotification.Body := Body;
end
else
inherited AssignTo(Dest);
end;
constructor TGCMNotificationMessage.Create;
begin
Body := '';
end;
{$ENDIF}
end.
感谢您的帮助..
【问题讨论】:
-
gcmn 在哪里实例化。为什么在应用程序上使用 FreeAndNil?那肯定是错的?
-
gcmn 在 notification.pas 中,我编辑了我的帖子
-
哦,好吧,我忘了。但是另一个问题,我如何关闭应用程序?FreeAndNil on Application(是不是错了?)
-
好的,非常感谢:)
-
实际上,我不确定关闭主窗体是否适用于 Android。该问题已在此处讨论:stackoverflow.com/questions/19234502/…
标签: android delphi mobile firemonkey