<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_DATE_CHANGED"/>
</intent-filter>
</receiver>
Manifests
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: not allowed to send broadcast android.intent.action.DATE_CHANGED from pid=3604, uid=10091
I get an error like this I searched but couldn't find a solution Please help...
Try this.
You cannot use ACTION_DATE_CHANGED
because it's a private android action, so you need to create your custom action.
<receiver
android:name=".MyReceiver"
android:exported="false">
<intent-filter>
<action android:name="${applicationId}.receiver.RECEIVE_MESSAGE_ACTION" />
</intent-filter>
</receiver>
Then you need to register for your activity before using it. Usually, I put in the onCreate method.
private fun registerReceiver(){
val filter = IntentFilter()
filter.addAction(MyReceiver.RECEIVE_MESSAGE_ACTION)
val customReceiver = MyReceiver()
registerReceiver(customReceiver, filter)
}
And when you called to share your message, use the following code.
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)
}