我已经实现了一个扩展NotificationListenerService的类,它适用于拾取已发布的通知.
我当时想要接收statusBarNotification对象并广播它.
我会做以下事情:
@Override
public void onNotificationPosted(StatusBarNotification statusBarNotification) {
Intent intent = new Intent();
intent.putExtra("STATUS_BAR_NOTIFICATION",statusBarNotification);
intent.setAction("com.example.NotificationPosted");
sendBroadcast(intent);
}
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我收到以下错误:
01-05 01:50:14.333 19574-19673/com.example W/NotificationListenerService[NotificationListener]? Error running onNotificationPosted
java.lang.RuntimeException: Not allowed to write file descriptors here
at android.os.Parcel.nativeAppendFrom(Native Method)
at android.os.Parcel.appendFrom(Parcel.java:431)
at android.os.Bundle.writeToParcel(Bundle.java:1679)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.app.Notification.writeToParcel(Notification.java:962)
at android.service.notification.StatusBarNotification.writeToParcel(StatusBarNotification.java:106)
at android.os.Parcel.writeParcelable(Parcel.java:1285)
at android.os.Parcel.writeValue(Parcel.java:1204)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.content.Intent.writeToParcel(Intent.java:7013)
at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2361)
at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
at com.example.NotificationListener.onNotificationPosted(NotificationListener.java:113)
at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:168)
at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到我做错了什么,或者这是不可能的.StatusBarNotification实现Parcelable
android android-intent android-notifications android-broadcast
我正在编写一个 Android 应用程序,其中我的服务需要将图像发送到其他一些应用程序(通过广播消息或启动服务 - 有多个应用程序可能有兴趣接收图像)。
如果我将图像加载到位图对象中并将其作为意图的“额外”,它实际上会起作用。但是,我想看看是否可以发送一个 ParcelFileDescriptor,并让客户端自行加载 Bitmap 对象(从阅读规范来看,ParcelFileDescriptor 似乎就是为了这个目的而创建的 - 在进程之间共享文件)。这里我试图避免通过 Intent 发送大对象。所以我写了这样的东西:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service is called" + this.getClass());
Intent newIntent = new Intent(MY_ACTION);
try {
File icon = new File(getExternalFilesDir(null), "robot_icon.jpg");
icon.setReadable(true, false);
if( !icon.exists() ) {
System.out.println("Writting file " + icon);
FileOutputStream out;
out = new FileOutputStream(icon);
BitmapFactory.decodeResource(getResources(), R.drawable.two_face_answer_map).compress(CompressFormat.JPEG, 100, out);
out.close();
System.out.println("Closing file after writing" + icon);
}
newIntent.putExtra(EXTRA_BITMAP, ParcelFileDescriptor.open(icon, ParcelFileDescriptor.MODE_READ_WRITE));
// sendBroadcast(newIntent);
startService(newIntent);
} catch (FileNotFoundException …Run Code Online (Sandbox Code Playgroud)