// 序列化PendingIntent对象,以时间作为文件名
String fileName = String.valueOf(ent.date);
PendingIntent intent = noti.contentIntent;
L.e("recordNotification", "保存文件名:" + fileName);
L.e("recordNotification", "PendingIntent:" + intent);
ParcelableFileUtil.writeParcelable(mContext, fileName, intent);
L.e("recordNotification", "保存通知并刷新界面");
Runnable runnable = new Runnable() {
@Override
public void run() {
NotificationWrapper.getInstance().addNotificationList(ent);
NotificationWrapper.getInstance().refreshNotification();
}
};
handler.post(runnable);
// cancelNotification(packageName, sbn.getTag(), sbn.getId());
我这是从通知栏里获取到一个通知的Intent,然后我想把它序列化保存到本地,在方法writeParcelable中出现报错。但搞不懂不知为什么出错。
writeParcelable方法如下,执行到byte[] data = parcel.marshall();返回二进制字节这里就出下面那个错了。
/**
* 存储单个Parcelable对象
*
* @param context
* @param fileName
* @param object
* @return boolean
*/
public static boolean writeParcelable(Context context, String fileName,
Parcelable object) {
if (fileName == null || object == null) {
return false;
}
boolean success = false;
FileOutputStream outPutStream = null;
try {
outPutStream = context.openFileOutput(fileName,
Context.MODE_PRIVATE);
Parcel parcel = Parcel.obtain();
parcel.writeParcelable(object,
Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
byte[] data = parcel.marshall();
outPutStream.write(data);
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
success = false;
} catch (IOException e) {
e.printStackTrace();
success = false;
} finally {
if (outPutStream != null) {
try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
错误日志代码:
08-26 03:07:38.023: E/onNotificationPosted(11563): packageName:com.example.notifications
08-26 03:07:38.043: E/recordNotification(11563): 系统通知View
08-26 03:07:38.113: E/Util(11563): saveBitmap:/storage/emulated/0/CleanMaster/notification_cache/IMG_20160826030738
08-26 03:07:38.113: E/recordNotification(11563): 保存文件名:1472180858021
08-26 03:07:38.113: E/recordNotification(11563): PendingIntent:PendingIntent{4238f680: android.os.BinderProxy@42355f30}
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): Error running onNotificationPosted
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): java.lang.RuntimeException: Tried to marshall a Parcel that contained Binder objects.
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at android.os.Parcel.nativeMarshall(Native Method)
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at android.os.Parcel.marshall(Parcel.java:420)
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at com.qiulong.notificationintercepttest.util.ParcelableFileUtil.writeParcelable(ParcelableFileUtil.java:36)
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at com.qiulong.notificationintercepttest.service.NotificationListener.getNotificationInfo(NotificationListener.java:125)
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at com.qiulong.notificationintercepttest.service.NotificationListener.onNotificationPosted(NotificationListener.java:58)
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:168)
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at android.os.Binder.execTransact(Binder.java:404)
08-26 03:07:38.113: W/NotificationListenerService[NotificationListener](11563): at dalvik.system.NativeStart.run(Native Method)