gpt4 book ai didi

android - getParcelableExtra 在 Pending Intent 上总是 null,但不是 Intent

转载 作者:行者123 更新时间:2023-11-29 15:20:00 30 4
gpt4 key购买 nike

我是 Java 的新手,更不用说 Android 开发了。我没有编码背景,但我想我会从像闹钟这样的“简单”东西开始。所以现在我正在写一个闹钟,如果闹钟响起后没有重复,我想将闹钟更新为非 Activity 状态。当我有意向将可打包的 AlarmClass(我的警报对象)传递到警报设置屏幕时,它通过得很好。当我将相同的 AlarmClass 传递给 Intent ,并将其放入警报管理器中的 PendingIntent 中,然后尝试从可以关闭警报的屏幕中获取它时,我正在调用和填充的 AlarmClass 对象完全相同始终为空。

这是我的闹钟类:

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];
}
};

从这里,我可以将我的对象传递给闹钟 setter 来更新它:

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

我在我的时间 setter 上收到它就很好。它是非空的:

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 发送时:

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();
}

并以同样的方式收到警报 Activity :

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 的可打包对象始终为空。

有谁知道为什么一个 parcelable 对象在 intent 中工作得很好,但在 PendingIntent 的另一边却变成 null?

最佳答案

关于android - getParcelableExtra 在 Pending Intent 上总是 null,但不是 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19215661/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com