记录下Android发送广播时,需要带权限发送的 shell 命令方法:
对应的android代码实现:
Context.sendBroadcast(someIntent, "SOME_PERMISSION");
涉及指令(在实机的shell下,否则可以把adb shell 加在前面):
am broadcast
使用参数:
--receiver-permission "SOME_PERMISSION"
具体示例:
如上面提到的代码实现,对应的串口命令就是:
am broadcast --receiver-permission "SOME_PERMISSION"
-----------------------------------分割线-----------------------------------
追记一些其他的参数使用方法和与代码的对应关系。
(1)实例化空的意图
Intent intent = new Intent();
(2)或者直接设置Action
Intent intent = new Intent("com.xxx.action.SOME_ACTION");
代码:一般会直接在实例化Intent对象的时候,配置上这个意图的Action像上面的(2)或用下面的方式给意图设置Action
// Action
intent.setAction("com.xxx.action.SOME_ACTION");
// Component
intent.setComponent(new Component("com.xxx.somepackage", "com.xxx.packagesclass"));
// Package
intent.setPackage("com.xxx.someReceivePackage");
// Extra-String
intent.putExtra("extraStringName", "stringValue");
// Extra-Int
intent.putExtra("extraIntName", 1);
// Extra-Boolean
intent.putExtra("extraBoolName", true);
...后续再补充
// Action
-a com.xxx.action.SOME_ACTION
// Component
-n com.xxx.somepackage/com.xxx.packagesclass
// Package
-p com.xxx.someReceivePackage
// Extra-String 注意String字符中存在的双引号需要做转义 \"
-es extraStringName "stringValue"
// Extra-Int
-ei extraIntName 1
// Extra-Boolean
-ez extraBoolName true
...后续再补充
代码:
Intent intent = new Intent();
intent.setAction("com.test.action.SOME_ACTION");
intent.setComponent(new Component("com.test.somepackage", "com.test.packagesclass"));
intent.setPackage("com.xxx.someReceivePackage");
intent.putExtra("compName", "Judis and \"SHOW TRANSFOR\" Company");
intent.putExtra("buildYear", 1993);
intent.putExtra("isStillRuning", true);
context.sendBroadcast(intent, "com.test.PERMISSION");
对应指令:
am broadcast -a com.test.action.SOME_ACTION -n com.test.somepackage/com.test.packagesclass -p com.xxx.someReceivePackage --receiver-permission "com.test.PERMISSION" -es compName "Judis and \"SHOW TRANSFOR\" Company" -ei buildYear 1993 -ez isStillRuning true