3

我有一个应用程序,它使用 AlarmManager 为用户通知安排警报。我将 Alarm 对象的一个​​实例传递给 AlarmManager 使用的 PendingIntent:

public void scheduleAlarms(List<Alarm> alarms)
{
    for (Alarm alarm : alarms)
    {           
        Log.d(Constants.EVERY_OTHER_ALARM_APP_LOG_TAG, 
                "Setting alarm for: " + new Date(alarm.getTime()).toString() + 
                " with an interval of " + 
                IntervalEnumStringProvider.getIntervalStringInGivenFormat(
                        alarm.getInterval(), 
                        true));
        PendingIntent alarmScheduledPending = createPendingIntent(alarm);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                alarm.getTime(), 
                (alarm.getInterval() * Constants.ONE_SECOND_IN_MILLIS), 
                alarmScheduledPending);
    }
}

private PendingIntent createPendingIntent(Alarm alarm)
{
    alarmBroadcastReceiverIntent = new Intent(context, AlarmBroadcastReceiver.class);
    alarmBroadcastReceiverIntent.putExtra(Constants.SCHEDULED_ALARM_TAG, alarm);
    return PendingIntent.getBroadcast(context, 
                alarm.getIdForPendingIntent(), 
                alarmBroadcastReceiverIntent, 
                PendingIntent.FLAG_UPDATE_CURRENT);
}

当闹钟时间发生时,我的AlarmBroadcastReceiver班级正确接收到广播,但 LogCat 警告说没有找到闹钟班级:

06-29 08:30:00.084: W/Intent(66): Failure filling in extras
06-29 08:30:00.084: W/Intent(66): java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializable object (name = com.fastplanet.everyotheralarmapp.alarm.Alarm)
06-29 08:30:00.084: W/Intent(66):   at android.os.Parcel.readSerializable(Parcel.java:1890)
06-29 08:30:00.084: W/Intent(66):   at android.os.Parcel.readValue(Parcel.java:1761)
06-29 08:30:00.084: W/Intent(66):   at android.os.Parcel.readMapInternal(Parcel.java:1947)
06-29 08:30:00.084: W/Intent(66):   at android.os.Bundle.unparcel(Bundle.java:169)
06-29 08:30:00.084: W/Intent(66):   at android.os.Bundle.putAll(Bundle.java:242)
06-29 08:30:00.084: W/Intent(66):   at android.content.Intent.fillIn(Intent.java:4530)
06-29 08:30:00.084: W/Intent(66):   at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:185)
06-29 08:30:00.084: W/Intent(66):   at android.app.PendingIntent.send(PendingIntent.java:400)
06-29 08:30:00.084: W/Intent(66):   at com.android.server.AlarmManagerService$AlarmThread.run(AlarmManagerService.java:636)
06-29 08:30:00.084: W/Intent(66): Caused by: java.lang.ClassNotFoundException: com.fastplanet.everyotheralarmapp.alarm.Alarm
06-29 08:30:00.084: W/Intent(66):   at java.lang.Class.classForName(Native Method)
06-29 08:30:00.084: W/Intent(66):   at java.lang.Class.forName(Class.java:237)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2595)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1848)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:852)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2006)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:956)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2289)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2243)
06-29 08:30:00.084: W/Intent(66):   at android.os.Parcel.readSerializable(Parcel.java:1884)
06-29 08:30:00.084: W/Intent(66):   ... 8 more
06-29 08:30:00.084: W/Intent(66): Caused by: java.lang.NoClassDefFoundError: com.fastplanet.everyotheralarmapp.alarm.Alarm
06-29 08:30:00.084: W/Intent(66):   ... 18 more
06-29 08:30:00.084: W/Intent(66): Caused by: java.lang.ClassNotFoundException: com.fastplanet.everyotheralarmapp.alarm.Alarm in loader dalvik.system.PathClassLoader@4001ad90
06-29 08:30:00.084: W/Intent(66):   at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-29 08:30:00.084: W/Intent(66):   at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-29 08:30:00.084: W/Intent(66):   at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-29 08:30:00.084: W/Intent(66):   ... 18 more

但是,BroadcastReceiver 成功地从接收到的意图中解包警报对象,并且应用程序按预期工作。

这是怎么回事?

更新 - 添加清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fastplanet.everyotheralarmapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="4" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:name=".data.TheEveryOtherAlarmAppApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name="com.fastplanet.everyotheralarmapp.alarm.AlarmBroadcastReceiver" >
        </receiver>
        <receiver android:name=".alarm.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

        <activity android:name=".TheEveryOtherAlarmAppActivity" 
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NewAlarmActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
        <activity
            android:name=".About"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
        <activity
            android:name=".AlarmAlertDialogActivity"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:taskAffinity=""
            android:theme="@android:style/Theme.NoDisplay" >
        </activity>
        <activity
            android:name=".AlarmDetailsActivity" 
            android:screenOrientation="portrait" >
        </activity>
    </application>
</manifest>

更新 2 - 添加警报类

public class Alarm implements Serializable
{

    private static final long serialVersionUID = -5410846652701834321L;
    private int id = 0;
    private String name = "";
    private long interval = 0;
    private long time;
    private String description;

    public Alarm(String name, long time, long interval, String description, int id)
    {
        this.name = name;
        this.time = time;
        this.interval = interval;
        this.description = description;
        this.id = id;
    }

    public void setInterval(long newAlarmInterval)
    {
        this.interval = newAlarmInterval;
    }

    public long getInterval()
    {
        return interval;
    }

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

    public long getTime()
    {
        return time;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setDescription(String newAlarmDescription)
    {
        this.description = newAlarmDescription;
    }

    public String getDescription()
    {
        return description;
    }

    public int getIdForPendingIntent()
    {
        return id;
    }

    @Override
    public String toString()
    {
        return "name: " + name + ", time: " 
            + new Date(time).toLocaleString()
            + ", interval: "
            + IntervalEnumStringProvider.getIntervalStringInGivenFormat(interval, true)
            + ", description: " + description
            + ", id: " + id;
    }

    @Override
    public Alarm clone() throws CloneNotSupportedException
    {
        return new Alarm(new String(name), time, interval, new String(description), id);
    }
}
4

5 回答 5

8

我在这个问题上苦苦挣扎。解决方案是(感谢mazur here):

这是安卓的缺陷。我的目标 API 是 19,最小 API 是 11。缺陷在这里输入:“ClassNotFoundException when using custom Parcelable” https://code.google.com/p/android/issues/detail?id=6822

注册警报时:

Bundle bundle = new Bundle();
bundle.putParcelable("com.foo.alarm", alert);
mNotificationReceiverIntent.putExtra("com.foo.alarm",bundle);
mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(this,
        0, mNotificationReceiverIntent, 0);

接到报警时,

    Bundle oldBundle = intent.getBundleExtra("com.foo.alarm");      
    Alert alert = oldBundle.getParcelable("com.foo.alarm");
于 2014-03-12T02:15:15.970 回答
0

作为

ClassNotFoundException: com.fastplanet.everyotheralarmapp.alarm.Alarm in loader

<receiver android:name=".alarm.BootReceiver" >
于 2012-06-29T08:41:09.083 回答
0

我有同样的问题,Android 2.3。

在我进行以下更改后,现在将可序列化的额外内容传递给 BroadcastReceiver 对我有用:

  • 为 PendingIntent 创建一个带有 Action 字符串的 Intent。
  • 在 AndroidManifest 中静态注册我的 BroadcastReceiver。
于 2013-04-01T23:14:10.107 回答
0

使用 ADT17,我遇到了同样的问题,但 BroadcastReceiver 无法启动。我尝试了关于 SO 讨论的内容:尝试更改 Proguard 配置,但结果证明Proguard 在默认情况下处于禁用状态。试图实施Parcelable,但问题仍然存在。唯一可行的方法是将 BroadCastReceiver 放入待接收对象的类所在的同一包中。请注意,正如 OP 所描述的那样,异常仍然作为警告抛出。

于 2012-12-17T16:43:32.610 回答
0

如果您想在 Intent 中将警报对象作为 EXTRA 传递,那么您的警报类需要实现 Parcelable,而不是 Serializable。

编辑:

似乎这不是真的。看起来您可以使用可序列化。但是,OP 无法使其正常工作,并且在 SE 上还有其他帖子,人们在使其正常工作时遇到了问题。

于 2012-06-29T08:55:43.843 回答