android 广播权限

android broadcast permission

   <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_DATE_CHANGED"/>
            </intent-filter>
        </receiver>

清单

 var intent2 = Intent(requireActivity(), MyReceiver::class.java)
        intent2.putExtra("Test", "value")
        intent2.setAction(Intent.ACTION_DATE_CHANGED)





  override fun onReceive(context: Context, intent: Intent) {
            var st = intent.getStringExtra("Test")
        
            if (intent.action.equals(Intent.ACTION_DATE_CHANGED)) {
                var st = intent.getStringExtra("Test")
                var toast = Toast.makeText(context, st, Toast.LENGTH_LONG) //
                toast.show()
            }

}

java.lang.SecurityException: Permission Denial: 不允许从 pid=3604, uid=10091

发送广播 android.intent.action.DATE_CHANGED

我收到这样的错误 我搜索但找不到解决方案 请帮忙...

试试这个。

您不能使用 ACTION_DATE_CHANGED,因为它是私有 android 操作,因此您需要创建自定义操作。

 <receiver
            android:name=".MyReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="${applicationId}.receiver.RECEIVE_MESSAGE_ACTION" />
            </intent-filter>
        </receiver>

那么您需要先注册您的 activity 才能使用它。通常,我放在 onCreate 方法中。

private fun registerReceiver(){
        val filter = IntentFilter()
        filter.addAction(MyReceiver.RECEIVE_MESSAGE_ACTION)

        val customReceiver = MyReceiver()
        registerReceiver(customReceiver, filter)
    }

当您打电话分享您的消息时,请使用以下代码。

companion object {
        const val RECEIVE_MESSAGE_ACTION = "${BuildConfig.APPLICATION_ID}.receiver.RECEIVE_MESSAGE_ACTION"
    }

private fun callCustomBroadcast(message: String) {
        val intent = Intent(RECEIVE_MESSAGE_ACTION)
        intent.putExtra("mymessage", message)
        sendBroadcast(intent)
    }