德尔福通过AIDL连接打印服务



我想在Delphi中使用这个Android代码:

Intent intent = new Intent();
intent.setPackage("com.sunmi.extprinterservice");
intent.setAction("com.sunmi.extprinterservice.PrinterService");
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
ServiceConnection serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
ExtPrinterService interface = ExtPrinterService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name)
{
}
};

使用接口对象实现自己的打印任务

interface.printText(“123456n”);

使用完成后解绑服务

unbindService(serviceConnection);

这是我第一次尝试。我没有办法测试这个。我也没有办法访问ExtPrinterService.这显然是另一回事,可能是清单声明和库导入步骤。至少,一旦你理解了如何使用接口,你可以看到一些Java代码以直接的方式转换为Delphi。

uses
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
Androidapi.Helpers,
Androidapi.JNIBridge,
Androidapi.JNI.App,
Androidapi.JNI.Os
procedure TTabbedForm.FormCreate(Sender: TObject);
var
Intent: JIntent;
ServiceConnection: JServiceConnection;
begin
Intent := TJIntent.Create;
Intent.setPackage(StringtoJString('com.sunmi.extprinterservice'));
Intent.setAction(StringtoJString('com.sunmi.extprinterservice'));
ServiceConnection := TJServiceConnection.Create;
ServiceConnection.onServiceConnected := OnServiceConnected;
ServiceConnection.onServiceDisconnected := OnServiceDisconnected;
SharedActivityContext.bindService(Intent, ServiceConnection, TJContext.JavaClass.BIND_AUTO_CREATE);
end;
procedure TTabbedForm.OnServiceConnected(name: JComponentName; Binder: JIBinder);
begin
//
end;
procedure TTabbedForm.OnServiceDisconnected(name: JComponentName);
begin
//
end;

最新更新