Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Delphi中以编程方式拒绝(挂断)Android上的来电?_Android_Delphi_Phone Call_Delphi 10 Seattle - Fatal编程技术网

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

如何在Delphi中以编程方式拒绝(挂断)Android上的来电?,android,delphi,phone-call,delphi-10-seattle,Android,Delphi,Phone Call,Delphi 10 Seattle,Java解决方案不是问题: public boolean killCall(上下文){ 试一试{ //找个无聊的老电话管理员 电话管理器电话管理器= (TelephonyManager)context.getSystemService(context.TELEPHONY_服务); //获取getITelephony()方法 Class classTelephony=Class.forName(telephonyManager.getClass().getName()); 方法methodGetI

Java解决方案不是问题:

public boolean killCall(上下文){
试一试{
//找个无聊的老电话管理员
电话管理器电话管理器=
(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
//获取getITelephony()方法
Class classTelephony=Class.forName(telephonyManager.getClass().getName());
方法methodGetITelephony=classTelephony.getDeclaredMethod(“getITelephony”);
//忽略该方法应该是私有的
methodGetItemPhony.setAccessible(true);
//调用getITelephony()以获取ITelephony接口
对象telephonyInterface=methodGetITelephony.invoke(telephonyManager);
//从ITelephony获取endCall方法
类电话接口类=
类.forName(telephonyInterface.getClass().getName());
方法methodEndCall=telephonyInterfaceClass.getDeclaredMethod(“endCall”);
//调用endCall()
调用(电话接口);
}catch(Exception ex){//反射调用可能会出现很多问题
Log.d(标记“PhoneStateReceiver**”+ex.toString());
返回false;
}
返回true;
}
但如何在Delphi中生成相同的解决方案?


不幸的是,我没有找到任何解决此问题的指南。

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

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

更新:在西雅图,大多数缺少的方法现在已添加到
Jlang_类中
。但是,
getDeclaredMethods()
不是其中之一,但幸运的是添加了
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;

我很抱歉问你为什么不使用java代码?@PedroLobito,因为他是用Delphi而不是java进行开发的?我需要一个用Delphi编写(而且必须是)的应用程序。我没有听说过用Delphi编写的应用程序在android上运行,android在很大程度上是基于java、DalvikVM或ART运行时的。@t0mm13b现在你有了。Delphi是跨平台的,可以编译成Win、iOS、OSX和Android。我有德尔福10西雅图。你有什么可以帮我的吗?如果bug在DELPHI 10 SEATTLE?@pudnivec74中,我会试试。如果你仔细观察,
Jlang_Class
仍然缺少Android类型中定义的几个方法,包括
getDeclaredMethod()
。但是,它现在确实有了
getDeclaredMethods()
,它应该是
getDeclaredMethod()
的可用替代品,方法是在每个
JMethod
上循环调用返回的数组
getName()
,直到找到所需的方法名。对于
JMethod.setAccessible
,必须使用
JAccessibleObject(JMethod).setAccessible(True)
。在您的例子中,
JAccessibleObject(methodGetITelephony).setAccessible(True)
看起来像是
invoke()
在Delphi中还不可用,所以在Embarcadero进一步充实JNI桥之前,这个问题仍然无法解决。
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;