Alarm manager with broadcast receiver not working on Fragment

Viewed 27

I'm trying to create a todo app with notifications (using: alarm manager and a broadcast receiver). The notification does not appear and the app crashes after ~1min or less. I believe the error cause is related with my handling of the alarm manager (it is my first time using it).

Any help will be appreciated

class alarmreciver:BroadcastReceiver() {
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(p0: Context?, p1: Intent?) {
        val Intent=Intent(p0,addfragment::class.java)
        val pending= TaskStackBuilder.create(p0!!)
            .addNextIntentWithParentStack(Intent)
            .getPendingIntent(0,0)
        val notification= Notification.Builder(p0,"channel1")
            .setContentTitle("task")
            .setContentText("done")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pending)
            .setAutoCancel(true)
            .build()
         val manager=p0!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        manager.notify(0,notification)
    }
} 

when i hide the createalarm method the app does not crash

   ofonbutton.setOnClickListener {
            if (ofonbutton.text=="ON"){
                date.visibility=View.GONE
                datedays.visibility=View.GONE
                datehour.visibility=View.GONE
                ofonbutton.text="OFF"
            }else{
                ofonbutton.text="ON"
                chosetime()
            }
        }
    }
    private fun chosetime() {
        var hours=0
        var mins=0
        val datecaldender=Calendar.getInstance()
        val year=datecaldender.get(Calendar.YEAR)
        val month=datecaldender.get(Calendar.MONTH)
        val day=datecaldender.get(Calendar.DAY_OF_MONTH)

        val datespicker=DatePickerDialog(requireActivity(),DatePickerDialog.OnDateSetListener { datePicker, i, i2, i3 ->
            requireActivity().findViewById<TextView>(R.id.date).visibility=View.VISIBLE
            requireActivity().findViewById<TextView>(R.id.datebldays).apply {
                visibility=View.VISIBLE
                text="$i/$i2/$i3"
            }
        },year,month,day)

        val timerpicker=TimePickerDialog(requireActivity(),TimePickerDialog.OnTimeSetListener { timePicker, i, i2 ->
            hours=timePicker.hour
            mins=timePicker.minute
            val hour= requireActivity().findViewById<TextView>(R.id.dateblhours)
            hour.visibility=View.VISIBLE
            hour.setText("at  ${timePicker.hour}:${timePicker.minute}")
        },0,0,false)
        timerpicker.show()
        datespicker.show()
            createchannel()
        val calenderalarm=Calendar.getInstance()
        calenderalarm[Calendar.YEAR]=datespicker.datePicker.year
        calenderalarm[Calendar.MONTH]=datespicker.datePicker.month
        calenderalarm[Calendar.DAY_OF_MONTH]=datespicker.datePicker.dayOfMonth
        calenderalarm[Calendar.HOUR]=hours
        calenderalarm[Calendar.MINUTE]=mins
        createalarm(calenderalarm.timeInMillis)
    }
    private fun createchannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val channel=NotificationChannel("channel1","channelk1",NotificationManager.IMPORTANCE_DEFAULT)
                channel.enableVibration(true)
            val manager=requireActivity().getSystemService(NotificationManager::class.java)
            manager.createNotificationChannel(channel)
        }
    }
    private fun createalarm(time:Long) {
        alarm=requireActivity().getSystemService(ALARM_SERVICE) as AlarmManager
        val intent=Intent(requireActivity(),alarmreciver::class.java)
        val pending=PendingIntent.getBroadcast(requireActivity(),0,intent,0)
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,time,AlarmManager.INTERVAL_DAY,pending)
    }

it shows this in logcat

java.lang.RuntimeException: Unable to start receiver com.example.todoapp.uivmodel.alarmreciver: java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.example.todoapp/com.example.todoapp.uivmodel.addfragment}
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3858)
        at android.app.ActivityThread.access$1500(ActivityThread.java:227)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7590)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.example.todoapp/com.example.todoapp.uivmodel.addfragment}
        at androidx.core.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:213)
        at androidx.core.app.TaskStackBuilder.addNextIntentWithParentStack(TaskStackBuilder.java:146)
        at com.example.todoapp.uivmodel.alarmreciver.onReceive(alarmreciver.kt:21)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3849)
        at android.app.ActivityThread.access$1500(ActivityThread.java:227) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7590) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
     Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{com.example.todoapp/com.example.todoapp.uivmodel.addfragment}
        at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:469)
        at androidx.core.app.NavUtils.getParentActivityName(NavUtils.java:263)
        at androidx.core.app.NavUtils.getParentActivityIntent(NavUtils.java:197)
        at androidx.core.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:206)
        at androidx.core.app.TaskStackBuilder.addNextIntentWithParentStack(TaskStackBuilder.java:146) 
        at com.example.todoapp.uivmodel.alarmreciver.onReceive(alarmreciver.kt:21) 
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3849) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:227) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7590) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
0 Answers
Related