Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

How could i send information from an AlarmManager to a BroadcastReceiver?(Android studio/Java)

ve trying to programme Android applications with java and i've got a problem that i cannot find solution.When i try to run the code,on the moment of sendind the notification,the app just explode. Here is the code.

**AlarmManager**
private void startAlarm(Notificacion noti){
        Bundle bundle =new Bundle();
        bundle.putSerializable("notificacion",noti);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 11);
        AlarmManager manager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent;
        PendingIntent pendingIntent;
        intent=new Intent(this,AlarmReceiver.class);
        intent.putExtras(bundle);
        pendingIntent=PendingIntent.getBroadcast(this,0,intent,0);
        manager.setRepeating(AlarmManager.RTC_WAKEUP,SystemClock.elapsedRealtime()+3000,3000,pendingIntent);

        //manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
          //      AlarmManager.INTERVAL_DAY, pendingIntent);

    }
**AlarmReceiver**
public class AlarmReceiver extends BroadcastReceiver {
    protected final String CHANNEL_ID ="1234";
    protected final int NOTIFICACIONID =12345;
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle=intent.getExtras();
        Notificacion noti=(Notificacion) bundle.getSerializable("notificacion");
        String s=noti.dateTostring();
        createNotificationChannel(context);
        Notificacion notificacion=(Notificacion) bundle.getSerializable("notificacion");
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.plus)
                .setContentTitle(s)
                .setContentText("noti.getTitulo()")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(NOTIFICACIONID, builder.build());
    }
 ```
question from:https://stackoverflow.com/questions/65833828/how-could-i-send-information-from-an-alarmmanager-to-a-broadcastreceiverandroi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...