我是 Java 新手,更不用说 Android 开发了。我没有编码背景,但我想我会从一些“简单”的东西开始,比如闹钟。所以现在我正在编写一个闹钟,如果它们在响起后没有重复,我想将它们更新为非活动状态。当我带着一个意图将一个可打包的 AlarmClass(我的警报对象)传递到警报设置屏幕时,它通过就好了。当我将相同的 AlarmClass 传递给一个意图,并将其放在警报管理器中的 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];
}
};
从这里,我可以将我的对象传递给警报设置器来更新它:
public void startSet(View view){
Intent set = new Intent(this, SetAlarm.class);
set.putExtra("alarm", new AlarmClass());
startActivity(set);
}
我在这里的时间设定器上收到它就好了。它是非空的:
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();
}
并以与警报活动相同的方式收到:
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 对象始终为空。
有谁知道为什么一个可打包的对象在一个意图中工作得很好,但在 PendingIntent 的另一边却是空的?