gpt4 book ai didi

android - 如何在 Delphi 中以编程方式拒绝(挂断)Android 上的来电?

转载 作者:太空狗 更新时间:2023-10-29 16:00:58 27 4
gpt4 key购买 nike

Java 解决方案不是问题:

public boolean killCall(Context context) {
try {
// Get the boring old TelephonyManager
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

// Get the getITelephony() method
Class classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(true);

// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

// Get the endCall method from ITelephony
Class telephonyInterfaceClass =
Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");

// Invoke endCall()
methodEndCall.invoke(telephonyInterface);

} catch (Exception ex) { // Many things can go wrong with reflection calls
Log.d(TAG,"PhoneStateReceiver **" + ex.toString());
return false;
}
return true;
}

但是如何在 Delphi 中做出相同的解决方案呢?

很遗憾,我没有找到任何解决此问题的指南。

最佳答案

不幸的是,由于 Delphi 的 Android Bridge 框架中的一个已知错误,您所要求的目前无法直接在 Delphi 代码中实现:

QC #120233 Android Jlang_Class interface is missing 19 methods

QP #RSP-12686 Android Jlang_Class interface is missing 19 methods

getDeclaredMethod() 是缺少的方法之一,没有它您将无法访问 ITelephony 接口(interface)。因此,根据 Embarcadero 的文档,您只需用 Java 代码编写应用程序的这一部分,将其包装在 .jar 文件中,然后作为外部 API 导入到您的 Delphi 代码中:

Using a Custom Set of Java Libraries In Your RAD Studio Android Apps

更新:在西雅图,大部分缺失的方法现已添加到Jlang_Class。然而,getDeclaredMethod() 不是其中之一,但幸运的是 getDeclaredMethods() 已被添加,因此您应该能够为此编写一个小的包装器,例如:

function getdeclaredMethod(Cls: Jlang_Class; const Name: JString): JMethod;
var
Arr: TJavaObjectArray<JMethod>;
Meth: JMethod;
I: Integer;
begin
Result := nil;
Arr := Cls.getDeclaredMethods;
for I := 0 to Arr.Length-1 do
begin
Meth := Arr.Items[I];
if Meth.getName.compareTo(Name) = 0 then
begin
Result := Method;
Exit;
end;
end;
raise Exception.CreateFmt('method not found: %s', [Name]);
end;

然后你可以这样做:

function killCall(context: JContext): Boolean;
var
obj: JObject;
telephonyManager: JTelephonyManager;
classTelephony: Jlang_Class;
methodGetITelephony: JMethod;
telephonyInterface: JObject;
telephonyInterfaceClass: Jlang_Class;
methodEndCall: JMethod;
begin
try
// Get the boring old TelephonyManager
obj := context.getSystemService(TJContext.JavaClass.TELEPHONY_SERVICE);
telephonyManager := TJTelephonyManager.Wrap((obj as ILocalObject).GetObjectID);

// Get the getITelephony() method
classTelephony := TJlang_Class.JavaClass.forName(telephonyManager.getClass.getName);
methodGetITelephony := getDeclaredMethod(classTelephony, StringToJString('getITelephony'));

// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(True);

// Invoke getITelephony() to get the ITelephony interface
telephonyInterface := methodGetITelephony.invoke(telephonyManager);

// Get the endCall method from ITelephony
telephonyInterfaceClass := TJlang_Class.JavaClass.forName(telephonyInterface.getClass.getName);
methodEndCall := getDeclaredMethod(telephonyInterfaceClass, StringToJString('endCall'));

// Invoke endCall()
methodEndCall.invoke(telephonyInterface);

Result := True;
except
on E: Exception do // Many things can go wrong with reflection calls
begin
//
Result := False;
end;
end;
end;

关于android - 如何在 Delphi 中以编程方式拒绝(挂断)Android 上的来电?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266447/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com