工作过程中一些android相关bug总结

此文档持续更新,主要记录工作或者学习过程中的一些bug

1.广播接收不到

——静态广播收不到

(1)intent写法不对,需要以下写法都对

//写法1
intentFilter.setPackage("com.example.otherprocess")
//写法2
intentFilter.setClassName("com.example.otherprocess","com.example.otherprocess.StaticBroadcastReceiver")
//写法3
intentFilter.setComponent(ComponentName("com.example.otherprocess","com.example.otherprocess.StaticBroadcastReceiver"))

注意:由于是静态广播,所以一定要设置package、或者接收器路径!!!

(2)权限匹配不正确
这也是最容易出现的问题,最常见的问题就是接收方和发送方的权限声明和定义搞错了!!!注意哈广播发送可以设置权限(receiverPermission)、接收器也可以设置权限(broadcastPermission),这是两个权限,
不要搞混了!
不要搞混了!!
不要搞混了!!!

简单来说,谁率先代码中引用了这个权限,那么它这个应用就是声明权限方,另一方就需要申请权限
正确写法如下:
receive接收方:

<permission android:name="com.exmaple.mybroadcast_permission"/> <!--声明权限,接收器引用,发送器申请-->
<receiver
	android:name=".StaticBroadcastReceiver"
    android:enabled="true"
    android:exported="true"
    android:permission="com.exmaple.mypermission">
    <intent-filter>
    	<action android:name="send_broadcast" />
    </intent-filter>
</receiver>

<uses-permission android:name="com.exmaple.myreceiver_permission"/>  <!--申请发送器定义权限-->

send发送方:

<permission android:name="com.exmaple.myreceiver_permission"/>  <!--声明权限,发送器引用,接收器申请-->
sendBroadcast(intentFilter,"com.exmaple.myreceiver_permission")

<uses-permission android:name="com.exmaple.mybroadcast_permission"/> <!--申请接收器定义权限-->

最后还有一点,就是权限命名要规范,不能乱写,推荐用包名+名字方式,如果乱写比如用自定义的单个字符串就不行。比如如下几个定义的权限是无效的

<permission android:name="demo"/>
<permission android:name="exampledemo"/>
<permission android:name="123.exampledemo"/>

(3)后续有新case更新……

——动态广播收不到

(1)权限匹配不正确
同上静态广播案例,还是要提醒一下!广播权限一定要注意好配对!!权限名不要乱写!!!

(2)匹配规则不正确
除了action简单的匹配,还有一些隐式规则没有匹配上
比如以下代码

receiver接收方
val filter = IntentFilter("send_broadcast")
filter.addDataScheme("https")
filter.addDataAuthority("com.example","8080")
this.registerReceiver(receiver, filter,"com.exmaple.mybroadcast_permission",null,RECEIVER_EXPORTED )
send发送方
intentFilter.setData(Uri.parse("https://com.example:8080"))
sendBroadcast(intentFilter,permissions)

发送方setData,然后接收方根据data进行过滤匹配
https:对应dataScheme
com.example:对应host
8080:对应port

一般data对应三部分
scheme://host:port/path 模式://主机:端口/路径

(3)设置了package、className、Component
动态广播不需要设置,非要设置的话设置一个package即可,不过会导致只有此进程能接受到你的广播

//可以收到
intentFilter.setPackage("com.example.otherprocess")
//收不到
intentFilter.setClassName("com.example.otherprocess","com.example.otherprocess.MyReceiver")
//收不到
intentFilter.setComponent(ComponentName("com.example.otherprocess","com.example.otherprocess.MyReceiver"))
sendBroadcast(intentFilter)

(4)后续有新case更新……

2.service绑定不上去

(1)权限没申请
service的权限,需要在调用方(客户端)的manifest中配置,不过此问题容易发现,因为如果没有权限会报java.lang.SecurityException异常

(2)其他地方权限没匹配好
服务端声明的一切权限,客户端至少有一个需要匹配上
比如下面server端声明了这么多权限:

<permission android:name="_123.exampledemo"/>
<permission android:name="com.huawei" /> 
<permission android:name="com.example.leo"/>

那么客户端至少申请了一个权限,否则两端没有权限绑定,bindService找不到
client端:

<uses-permission android:name="_123.exampledemo" />
<uses-permission android:name="com.huawei" />
<uses-permission android:name="com.example.leo" />

注意踩坑点:由于权限是和app状态绑定的,所以每次要修改权限的代码,最好卸载app重新安装

(3)后续有新case更新

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C_lea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值