场景:银行及政府自助签署授权书一体机上面,硬件交互以及和业务无关的功能单独在另个apk中实现,在业务apk中只处理业务 如下图:
编辑
1. 服务端apk生成自定义PDF
1.添加依赖
implementation 'com.itextpdf:itext7-core:7.1.13'
编辑
2.客户端链接服务及调用生成pdf接口
package com.zchd.sign.pdf.client
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.Environment
import android.os.IBinder
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.PermissionUtils
import com.zchd.sign.pdf.service.SignPdfAidlInterface
import com.zchd.sign.pdf.service.SignPdfCallback
import com.zchd.sign.pdf.service.SignPdfInfo
class MainActivity : AppCompatActivity() {
private var pdfAidlInterface: SignPdfAidlInterface? = null
private val serviceConnection: ServiceConnection = object : ServiceConnection {
override fun onServiceConnected(componentName: ComponentName, iBinder: IBinder) {
pdfAidlInterface = SignPdfAidlInterface.Stub.asInterface(iBinder)
Toast.makeText(this@MainActivity, "链接成功", Toast.LENGTH_LONG).show()
}
override fun onServiceDisconnected(componentName: ComponentName) {
Toast.makeText(this@MainActivity, "链接失败", Toast.LENGTH_LONG).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (!PermissionUtils.isGranted(*PermissionActivity.permissions.toTypedArray())) {
startActivity(
Intent(this, PermissionActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
findViewById<Button>(R.id.btn_connect).setOnClickListener {
val intent = Intent()
intent.component = ComponentName("com.zchd.sign.pdf.service", "com.zchd.sign.pdf.service.SignPdfService"
)
bindService(intent, serviceConnection, BIND_AUTO_CREATE)
}
findViewById<Button>(R.id.btn_preview).setOnClickListener {
val signPdfInfo = SignPdfInfo(
userName = "王天一",
userCardType = "身份证",
userIdCard = "524000000000000",
houseRelation = "/",
repName = "测试",
repCardType = "身份证",
repIdCard = "123456789",
authRelation = "/",
signPicPath = null,
fingerPicPath = null,
repSignPicPath = null,
repFingerPicPath = null
)
val sdCardPath = Environment.getExternalStorageDirectory().absolutePath
val pdfPath = "${sdCardPath}/sign_pdf.pdf"
pdfAidlInterface?.sign(signPdfInfo, pdfPath, object : SignPdfCallback.Stub() {
override fun onSuccess(path: String?) {
Toast.makeText(this@MainActivity, "服务端生成pdf成功", Toast.LENGTH_LONG).show()
val intent = Intent(baseContext, WebViewActivity::class.java)
intent.putExtra("path", path)
startActivity(intent)
}
override fun onFail() {
Toast.makeText(this@MainActivity, "服务端生成pdf失败", Toast.LENGTH_LONG).show()
}
})
}
}
}
编辑
编辑
3.安卓上实现PDF预览 PDF.js 中文文档
pdfjs编译完后放在安卓assets 通过webView加载支持在线和本地pdf地址
webView.loadUrl("file:///android_asset/index.html?${path}")
项目地址https://gitee.com/firefix-m/aidl-sign-pdf