问题描述
我有 2 个应用程序 signprotectbroadcast 和 broadcastsender
在signprotectbroadcast中,我注册了一个接收器
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.signprotectbroadcast">
<permission android:name="PERMISSION_OP"
android:protectionLevel="signature"
android:label="PERMISSION">
</permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SignProtectbroadcast">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReciever"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedReceiver"
android:permission="PERMISSION_OP">
<intent-filter>
<action android:name="ACTION_OP" />
</intent-filter>
</receiver>
</application>
</manifest>
在应用程序broadcastsender中,我在清单
中请求权限<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastsender">
<uses-permission android:name="PERMISSION_OP"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.broadCastSender">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
并像这样发送广播
sendBtn.setonClickListener {
val intent = Intent()
intent.action = "ACTION_OP"
intent.component = ComponentName("com.example.signprotectbroadcast","com.example.signprotectbroadcast.MyReciever" )
sendbroadcast(intent,"PERMISSION_OP")
}
在运行发布版本变体时,我还制作了一个通用的密钥库,但这根本不起作用,尝试了所有方法。
一旦我从 receiver 块和 sendbroadCast
函数中删除了权限,广播就会被正确接收.
有人能指出我正确的方向在哪里调试为什么没有收到这个广播或如何调试这个?
解决方法
发送广播的应用需要声明权限,以及使用权限的状态;
发送应用:broadcastsender(声明和使用权限)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastsender">
<permission android:name="PERMISSION_OP"
android:protectionLevel="signature"
android:label="PERMISSION"/>
<uses-permission android:name="PERMISSION_OP"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BroadCastSender">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
用法:
sendBtn.setOnClickListener {
val intent = Intent()
intent.action = "ACTION_OP"
intent.component = ComponentName("com.example.signprotectbroadcast","com.example.signprotectbroadcast.MyReciever" )
sendBroadcast(intent,"PERMISSION_OP")
}
接收应用:signprotectbroadcast(仅使用权限)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.signprotectbroadcast">
<uses-permission android:name="PERMISSION_OP"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SignProtectBroadcast">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReciever"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedReceiver"
android:permission="PERMISSION_OP">
<intent-filter>
<action android:name="ACTION_OP" />
</intent-filter>
</receiver>
</application>
</manifest>