我是Java新手,更不用说Android开发了。我没有编码背景,但我想我会从像闹钟这样简单的东西开始。所以现在我正在写一个闹钟,如果它们在关闭后没有重复,我想更新闹钟。当我将一个可分辨的AlarmClass(我的警报对象)传递给具有意图的警报设置屏幕时,它通过就好了。 当我将相同的AlarmClass传递给一个intent,并将它放在Alarm Manager中的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对象总是为null。
有没有人知道为什么一个parcelable对象在intent中可以正常工作,但是在PendingIntent的另一端出现null?
答案 0 :(得分:4)
听起来您遇到的问题类似于ClassNotFoundException when using custom Parcelable
https://code.google.com/p/android/issues/detail?id=6822描述了一种解决方法 并回答ClassNotFoundException when using custom Parcelable
答案 1 :(得分:1)
事实证明,ParcelableExtra在广播接收器上掉线了!我确实正在通过它,但是,当广播接收器发射活动时,它没有重新附加附加内容;在尝试getParcelableExtras时,活动中出现null。
答案 2 :(得分:0)
编辑: 我认为你的问题就在这一行:
PendingIntent torpedo = PendingIntent.getBroadcast(this,alarmObject.getAlarmNumber(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
您没有在此处开始活动,但BroadCast尝试按如下方式更改活动:
PendingIntent torpedo = PendingIntent.getActivity(this,alarmObject.getAlarmNumber(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
将PendingIntent.getBroadcast()
更改为PendingIntent.getActivity()
;
尝试更改以下内容:
Intent intent = new Intent(getBaseContext(), Alarm.class);
到
Intent intent = new Intent(this , Alarm.class);
或
Intent intent = new Intent(getApplicationContext(), Alarm.class);
答案 3 :(得分:0)
试
boomAlarm = (AlarmClass) getIntent().getExtras().get("alarm");
而不是
boomAlarm = (AlarmClass) getIntent().getParcelableExtra("alarm");
如果不起作用
使AlarmClass扩展Serializable而不是Parceable。