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

由网友(奶瓶还我初吻)分享简介:一个Java解决方案是没有问题的:A Java solution is not a problem:public boolean killCall(Context context) {try {// Get the boring old TelephonyManagerTelephonyManager teleph...

一个Java解决方案是没有问题的:

A Java solution is not a problem:

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中相同的解决方案?

不幸的是我没有找到任何指导,以解决这个问题。

Unfortunately I did not find any a guide to solve this problem.

推荐答案

不幸的是,你所要求的是什么,目前不可能在德尔福code直接,由于德尔福的Andr​​oid桥框架中的已知错误:

Unfortunately, what you are asking for is currently not possible in Delphi code directly, due to a known bug in Delphi's Android Bridge framework:

QC#120233的Andr​​oid Jlang_Class接口丢失19方法

QP#RSP-12686的Andr​​oid Jlang_Class接口丢失19方法

getDeclaredMethod()是缺少的方法之一,没有它,你不能访问到 ITelephony 接口。所以,你只需要编写一个部分用Java code你的应用程序,把它包在的.jar 文件中,并导入到德尔福code作为外部API,每英巴卡迪诺的文档:

getDeclaredMethod() is one of the missing methods, and without it you cannot gain access to the ITelephony interface. So, you will just have to write that portion of your app in Java code, wrap it inside a .jar file, and import into your Delphi code as an external API, per Embarcadero's documentation:

Using一组自定义的Java库在你的RAD Studio Android应用

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

Update: in Seattle, most of the missing methods have now been added to Jlang_Class. However, getDeclaredMethod() is not one of them, but fortunately getDeclaredMethods() has been added, so you should be able to write a small wrapper for that, eg:

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;

然后你可以这样做:

And then you can do this:

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;
阅读全文

相关推荐

最新文章