實(shí)現(xiàn)方案
- 安卓監(jiān)聽(tīng)特定廣播,啟動(dòng)服務(wù)來(lái)讀寫安卓剪貼板
- 電腦端通過(guò)adb命令發(fā)送廣播調(diào)用安卓客戶端對(duì)應(yīng)服務(wù)
安卓主要代碼
class ClipperReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val cb = context
.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
if (isActionSet(intent.action)) {
val text = intent.getStringExtra(EXTRA_TEXT)
if (text != null) {
cb.setPrimaryClip(ClipData.newPlainText("", text))
resultCode = Activity.RESULT_OK
resultData = "Copied to phone."
} else {
resultCode = Activity.RESULT_CANCELED
resultData = "No text is provided. Use -e text \"text to be pasted\""
}
} else if (isActionGet(intent.action)) {
val clip: CharSequence = cb.primaryClip.toString()
resultCode = Activity.RESULT_OK
resultData = clip.toString()
}
}
private fun isActionGet(action: String?): Boolean {
return ACTION_GET == action || ACTION_GET_SHORT == action
}
private fun isActionSet(action: String?): Boolean {
return ACTION_SET == action || ACTION_SET_SHORT == action
}
}
使用方法
1. 安裝共享剪貼板app
https://gitee.com/cxyzy1/adbShareClipboard/blob/master/apks/app-debug.apk
2. 在手機(jī)上啟動(dòng)app
3. 將手機(jī)通過(guò)usb線連接到Mac
4. 修改手機(jī)剪貼板內(nèi)容:
電腦端執(zhí)行:
adb shell am broadcast -a clipper.set -e text "電腦上的內(nèi)容"
然后手機(jī)上就可以直接粘貼了.
5. 獲取手機(jī)剪貼板內(nèi)容:
電腦端執(zhí)行:
adb shell am broadcast -a clipper.get|grep data|cut -d"=" -f3|sed 's/\"http://g'|pbcopy
然后直接粘貼就可以了