2

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 中做出同样的解决方案呢?

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

4

1 回答 1

7

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

QC #120233 Android Jlang_Class 接口缺少 19 个方法

QP #RSP-12686 Android Jlang_Class 接口缺少 19 个方法

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

在您的 RAD Studio Android 应用程序中使用一组自定义 Java 库

更新:在西雅图,大部分缺失的方法现在已添加到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;
于 2015-10-21T19:51:48.430 回答