I want to start an activity from the onReceive()
method of the BroadcastReceiver. This is the code i am using:
class TimeReminderReceiver : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
println("RECEIVED")
val i = Intent(p0!!, TimeReminderActivity::class.java)
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
p0.startActivity(i)
}
}
There are many answers for this question in stackoverflow, i tried all of it and none of it is working for me. The app just prints RECEIVED
and stays there. Nothing shows at the logcat, no exception, nothing. I have added the receiver in the mainfest also.
<receiver
android:name=".receivers_and_activities.TimeReminderReceiver" />
Whats the problem with this code?
EDIT:
Code that calls the broadcast:
val intent = Intent(this@MainActivity, TimeReminderReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this@MainActivity, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
val am = this@MainActivity.getSystemService(Context.ALARM_SERVICE) as AlarmManager
am.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)