在有待执行的Intent上,getParcelableExtra始终为null,但在Intent上则不为null [英] getParcelableExtra always null on Pending Intent, but not Intent

查看:912
本文介绍了在有待执行的Intent上,getParcelableExtra始终为null,但在Intent上则不为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手,更不用说Android开发了.我没有编码背景,但以为我会从简单"的东西开始,例如闹钟.因此,现在我正在编写一个闹钟,如果要在它们响起后不再重复,我想将它们更新为不活动.当我故意将可拆分的AlarmClass(我的警报对象)传递到警报设置屏幕时,它通过就很好了. 当我将相同的AlarmClass传递给一个Intent,并将其放在Alarm Manager中的PendingIntent中,然后尝试从可以关闭该警报的屏幕上获取它时,我正在调用的AlarmClass对象并以完全相同的方式进行填充始终为空.

I'm new to Java let alone Android development. I have no coding background, but thought I'd start with something 'simple' like an alarm clock. So now I'm writing an alarm clock, and I want to update the alarms to be inactive if they aren't repeating after they go off. When I pass a parcelable AlarmClass (my alarm object) to the alarm set screen with an intent, it passes just fine. When I pass the same AlarmClass to an intent, and put it in a PendingIntent in the Alarm Manager and then try to get it from the screen where you can dismiss the alarm, the AlarmClass object that I'm calling and populating the exact same way is always null.

这是我的闹钟课程:

package com.example.wakeme;

import java.util.Calendar;

import android.os.Parcel;
import android.os.Parcelable;

public class AlarmClass implements Parcelable{
    Calendar cal = Calendar.getInstance();
    private long id = 1;
    private int active = 1;
    private long time = cal.getTimeInMillis();
    private int repeating = 0;
    private int skipweekends = 0;
    private int skipweekdays = 0;
    private int alarmnumber = -1;


    public long getId(){
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getActive(){
        return active;
    }

    public void setActive(int active) {
        this.active = active;
    }

    public int getRepeating(){
        return repeating;
    }

    public void setRepeating(int repeating){
        this.repeating = repeating;
    }

    public void setTime(long time){
        this.time = time;
    }

    public long getTime(){
        return time;
    }

    public int getSkipWeekends(){
        return skipweekends;
    }

    public void setSkipWeekends(int skipweekends){
        this.skipweekends = skipweekends;
    }

    public int getSkipWeekdays(){
        return skipweekdays;
    }

    public void setSkipWeekdays(int skipweekdays){
        this.skipweekdays = skipweekdays;
    }

    public void setAlarmNumber(int alarmnumber){
        this.alarmnumber = alarmnumber;
    }

    public int getAlarmNumber(){
        return alarmnumber;
    }

    @Override
    public int describeContents(){
        return 0;
    }

    private AlarmClass(Parcel in){
        this.id = in.readLong();
        this.active = in.readInt();
        this.repeating = in.readInt();
        this.skipweekdays = in.readInt();
        this.skipweekends = in.readInt();
        this.time = in.readLong();
        this.alarmnumber = in.readInt();
    }

    AlarmClass() {
        return;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeLong(this.id);
        out.writeInt(this.active);
        out.writeInt(this.repeating);
        out.writeInt(this.skipweekdays);
        out.writeInt(this.skipweekends);
        out.writeLong(this.time);
        out.writeInt(this.alarmnumber);
    }

    public static final Parcelable.Creator<AlarmClass> CREATOR = new Parcelable.Creator<AlarmClass>(){
        public AlarmClass createFromParcel(Parcel in){
            return new AlarmClass(in);
        }
        public AlarmClass[] newArray(int size) {
            return new AlarmClass[size];
        }
    };

从这里,我可以将对象传递给警报设置器以对其进行更新:

From here, I can pass my object to an alarm setter to update it just fine:

public void startSet(View view){
        Intent set = new Intent(this, SetAlarm.class);
        set.putExtra("alarm", new AlarmClass());
        startActivity(set);
    }

我在这里的时间安排器上就收到了它.它不是空的:

And I recieve it on my time setter right here just fine. It's non-null:

public class SetAlarm extends MainActivity {
    AlarmClass passAlarm = new AlarmClass();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm_set);
        setAlarmSetListener();
        setDeleteListener();
        passAlarm = (AlarmClass) getIntent().getParcelableExtra("alarm");
        if(passAlarm.getAlarmNumber() != -1){
            TimePicker picker = (TimePicker) findViewById(R.id.timePicker1);
            Calendar passedTime = Calendar.getInstance();
            passedTime.setTimeInMillis(passAlarm.getTime());
            picker.setCurrentHour(passedTime.get(Calendar.HOUR_OF_DAY));
            picker.setCurrentMinute(passedTime.get(Calendar.MINUTE));
            String ampm = (passedTime.get(Calendar.HOUR_OF_DAY) > 12 ? "pm" : "am");
            String message = "Passed Time is: " + (passedTime.get((Calendar.HOUR_OF_DAY)%12)!=0?(passedTime.get(Calendar.HOUR_OF_DAY)%12):12) + ":" + passedTime.get(Calendar.MINUTE) + " " + ampm;
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
        }
        else{
        // Do Nothing
        }

    }

但是当通过PendingIntent发送时:

But when sent via PendingIntent as so:

private void newAlarm(AlarmClass alarmclass){
        Intent intent = new Intent(getBaseContext(), Alarm.class);
        intent.putExtra("alarm", alarmclass);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(alarmclass.getTime());
        AlarmsDataSource alarm =  new AlarmsDataSource(this);
        alarm.open();
        AlarmClass alarmObject = new AlarmClass();
        alarmObject = alarm.createAlarm(true, cal.getTimeInMillis(), false, false, false);
        PendingIntent torpedo = PendingIntent.getBroadcast(this,alarmObject.getAlarmNumber(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager goOff = (AlarmManager) getSystemService(ALARM_SERVICE);
        goOff.set(AlarmManager.RTC_WAKEUP, alarmObject.getTime() ,torpedo);
        Toast.makeText(getApplicationContext(), "Alarm Number " + alarmObject.getAlarmNumber(), Toast.LENGTH_LONG).show();
        alarm.close();
    }

并以相同的方式接收警报活动:

And received in the same way on the alarm activity as so:

public class Boom extends MainActivity{
AlarmClass boomAlarm = new AlarmClass();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final Window wake = getWindow();
            wake.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            setContentView(R.layout.boom);
            setTheme(R.style.AlarmStyle);
            overridePendingTransition(android.R.anim.fade_in,5000);
            boomAlarm = (AlarmClass) getIntent().getParcelableExtra("alarm");
    //      Vibrator buzz = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    //      buzz.vibrate(1000);
            snoozeListener();
            dismissListener();
        }

我传递给boomAlarm的传递的可包裹对象始终为空.

The passed parcelable object that I'm passing into boomAlarm is always null.

有人知道为什么可打包对象在意图中可以正常工作,而在PendingIntent的另一端却为空吗?

Does anyone know why a parcelable object would work just fine in an intent, but come out null on the other side of a PendingIntent?

推荐答案

事实证明,ParcelableExtra已在广播接收器处掉落!但是,我确实是通过了它,但是,当广播接收器触发该活动时,它并没有重新附加这些附件.尝试获取getParcelableExtras时,导致活动为null.

It turns out that the ParcelableExtra was getting dropped at the broadcast receiver! I was indeed passing it, BUT, when the broadcast receiver fired off the activity, it did not re-attach the extras; resulting in a null in the activity when it tried to getParcelableExtras.

这篇关于在有待执行的Intent上,getParcelableExtra始终为null,但在Intent上则不为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆