使用Firemonkey在android中获取设备电子邮件地址

使用Firemonkey在android中获取设备电子邮件地址,android,delphi,firemonkey,delphi-10-seattle,delphi-10.1-berlin,Android,Delphi,Firemonkey,Delphi 10 Seattle,Delphi 10.1 Berlin,我正在使用Delphi 10西雅图开发android移动应用程序,我需要获取设备电子邮件Id。为此,我尝试使用以下示例,但当我尝试使用接口(JAccountClass、JAccount、JAccountManagerClass)时,它要求一些其他接口-JActivity,JAccountManagerCallback和JAccountManagerFuture。请帮我找到这些依赖类。我在哪里可以得到这些文件 提前感谢为了避免该存储库中代码的异常和琐碎,这里有一个在Delphi 10中工作的单元

我正在使用Delphi 10西雅图开发android移动应用程序,我需要获取设备电子邮件Id。为此,我尝试使用以下示例,但当我尝试使用接口(JAccountClass、JAccount、JAccountManagerClass)时,它要求一些其他接口-JActivity,JAccountManagerCallback和JAccountManagerFuture。请帮我找到这些依赖类。我在哪里可以得到这些文件


提前感谢

为了避免该存储库中代码的异常和琐碎,这里有一个在Delphi 10中工作的单元

请注意,指定的帐户类型可以注册多个帐户,这就是此代码返回帐户数组的原因

unit AccountEmailsU;

interface

function GetAccountEmails(const AccountType: String): TArray<String>;

implementation

uses
  Androidapi.Helpers,
  Androidapi.Jni,
{$IF Declared(RTLVersion) and (RTLVersion >= 31)}
  // Delphi 10.1 Berlin adds in full imports for the accounts classes
  Androidapi.JNI.Accounts;
{$ELSE}
  Androidapi.JNIBridge,
  Androidapi.JNI.App,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Os;

type
// ===== Forward declarations =====

  JAccount = interface;//android.accounts.Account
  JAccountManager = interface;//android.accounts.AccountManager

// ===== Interface declarations =====

  JAccountClass = interface(JObjectClass)
    ['{94EE6861-F326-489F-8919-E20B39E3D9C1}']
  end;

  [JavaSignature('android/accounts/Account')]
  JAccount = interface(JObject)
    ['{71476381-8B6E-471F-9189-9857ECD7508C}']
    function _Getname: JString; cdecl;
    function _Gettype: JString; cdecl;
    property name: JString read _Getname;
    property &type: JString read _Gettype;
  end;
  TJAccount = class(TJavaGenericImport<JAccountClass, JAccount>) end;

  JAccountManagerClass = interface(JObjectClass)
    ['{96273844-2D84-47F0-BFD5-14B73402F843}']
    {class} function &get(context: JContext): JAccountManager; cdecl;
  end;

  [JavaSignature('android/accounts/AccountManager')]
  JAccountManager = interface(JObject)
    ['{9FA4077B-4628-433C-BAFC-9EB299DA9C98}']
    function getAccountsByType(type_: JString): TJavaObjectArray<JAccount>; cdecl;
  end;
  TJAccountManager = class(TJavaGenericImport<JAccountManagerClass, JAccountManager>) end;
{$ENDIF}

function GetAccountEmails(const AccountType: String): TArray<String>;
var
  AccountManager: JAccountManager;
  Accounts: TJavaObjectArray<JAccount>;
  Account: JAccount;
  AccountLoopCounter: Integer;
begin
{$IF RTLVersion >= 30}
  AccountManager := TJAccountManager.JavaClass.get(TAndroidHelper.Context);
{$ELSE}
  AccountManager := TJAccountManager.JavaClass.get(SharedActivityContext);
{$ENDIF}
  if AccountManager <> nil then
  begin
    Accounts := AccountManager.getAccountsByType(StringToJString(AccountType));
    if Accounts <> nil then
    begin
      SetLength(Result, Accounts.Length);
      for AccountLoopCounter := 0 to Pred(Accounts.Length) do
      begin
        //Account := Accounts.Items[AccountLoopCounter];
        Account := TJAccount.Wrap(Accounts.GetRawItem(AccountLoopCounter));
        Result[AccountLoopCounter] := JStringtoString(Account.name);
      end
    end;
  end;
end;

procedure RegisterTypes;
begin
  TRegTypes.RegisterType('AccountEmailsU.JAccount', TypeInfo(AccountEmailsU.JAccount));
  TRegTypes.RegisterType('AccountEmailsU.JAccountManager', TypeInfo(AccountEmailsU.JAccountManager));
end;

initialization
  RegisterTypes;
end.
unitaccountsu;
接口
函数GetAccountEmails(const AccountType:String):TArray;
实施
使用
安德罗里达皮,帮手,
Androidapi.Jni,
{$IF声明(RTLVersion)和(RTLVersion>=31)}
//Delphi 10.1 Berlin为accounts类添加了完整导入
Androidapi.JNI.Accounts;
{$ELSE}
Androidapi.JNIBridge,
Androidapi.JNI.App,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os;
类型
//======转发声明=====
JAccount=接口//android.accounts.Account
JAccountManager=接口//android.accounts.AccountManager
//====接口声明=====
JAccountClass=接口(JObjectClass)
[{94EE6861-F326-489F-8919-E20B39E3D9C1}]
结束;
[JavaSignature('android/accounts/Account')]
JAccount=接口(JObject)
[{71476381-8B6E-471F-9189-9857ECD7508C}]
函数_Getname:JString;cdecl;
函数_Gettype:JString;cdecl;
属性名称:JString read\u Getname;
属性和类型:JString read\u Gettype;
结束;
TJAccount=类(TJavaGenericImport)结束;
JAccountManagerClass=接口(JObjectClass)
[{96273844-2D84-47F0-BFD5-14B73402F843}]
{class}函数&get(上下文:JContext):JAccountManager;cdecl;
结束;
[JavaSignature('android/accounts/AccountManager')]
JAccountManager=接口(JObject)
[{9FA4077B-4628-433C-BAFC-9EB299DA9C98}]
函数getAccountsByType(类型:JString):TJavaObjectArray;cdecl;
结束;
TJAccountManager=类(TJavaGenericImport)结束;
{$ENDIF}
函数GetAccountEmails(const AccountType:String):TArray;
变量
会计经理:会计经理;
账户:TJavaObjectArray;
账户:JAccount;
AccountLoopCounter:整数;
开始
{$IF RTLVersion>=30}
AccountManager:=TJAccountManager.JavaClass.get(tandrodhelper.Context);
{$ELSE}
AccountManager:=TJAccountManager.JavaClass.get(SharedActivityContext);
{$ENDIF}
如果AccountManager为零,则
开始
Accounts:=AccountManager.getAccountsByType(StringToJString(AccountType));
如果帐户为零,则
开始
SetLength(结果,Accounts.Length);
对于AccountLoopCounter:=0到Pred(Accounts.Length)do
开始
//Account:=Accounts.Items[AccountLoopCounter];
Account:=TJAccount.Wrap(Accounts.GetRawItem(AccountLoopCounter));
结果[AccountLoopCounter]:=JStringtoString(Account.name);
结束
结束;
结束;
结束;
程序注册类型;
开始
RegisterType('AccountEmailsU.JAccount',TypeInfo(AccountEmailsU.JAccount));
TRegTypes.RegisterType('AccountEmailsU.JAccountManager',TypeInfo(AccountEmailsU.JAccountManager));
结束;
初始化
注册类型;
结束。

尝试存储库。(检查这些,)我已经添加了上述文件,然后在编译时发现了一个错误,即找不到该文件-android.accounts.AccountManagerFuture、android.accounts.AccountManagerCallback、android.app.Activity、android.content.ClipData。因此,如果我添加这些文件,那么它会要求更多的依赖文件,如果我将搜索路径设置为as folder,那么我也会收到一些循环错误消息。请帮助解决此依赖性问题。了解如何编译此unitNote的用法示例,请参阅扩展答案中的说明,以确保您拥有正确的权限。在实现Android 26后,getAccountsByType不会返回任何值。。我怎样才能检索电子邮件?我将看看我能否在接下来的一两天内在设备上运行测试。同时,我要求,如果您认为上述答案在2017年就已经回答了您的问题,请您将其标记为该答案。