如何在自定义通知中添加自定义视图?

时间:2019-08-12 19:53:13

标签: android notifications

我尝试发出自定义通知,我可以在其中直接选择颜色。 为此,我需要向通知中添加自定义视图(或库组件)。

我找到了一种添加自定义布局作为通知的方法,但是为此,您需要一个<item> <title></title> <description></description> <author></author> <link></link> <pubDate></pubDate> </item> 实例,您可以在其中实例化布局。我在视频中看到[0] => SimpleXMLElement Object ( [title] => [description] => [author] => [link] => [pubDate] => ) 只能处理某种视图。

这是我到目前为止所拥有的(无法正常工作,可能是因为RemoteViews无法处理ColorPicker)

在我的片段中:(当激活切换按钮时)

//You have to start the Screen Capture based on an action. 
//mWidth, mHeight depends on device screen width, height
mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
mVirtualDisplay = sMediaProjection.createVirtualDisplay(SCREENCAP_NAME, mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler)
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);

//Then convert the media captured to an Image
sMediaProjection.stop();
image = mImageReader.acquireLatestImage();
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "SNAPSHOT.jpg");
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.close();

notification.xml

RemoteViews

所以... 不使用RemoteViews可以创建自定义通知吗?

1 个答案:

答案 0 :(得分:0)

我有类似的问题,RemoteViews不支持任何自定义类。 RemoteViews仅支持android基本组件,例如TextView,ImageView

我想显示具有非操作性的customView,只是将数据显示到customView中,例如显示身份卡之类的个人信息。

我是从customView布局创建位图的,因此计算measureSpec,然后将位图绘制到画布中,最后在通知布局中将位图设置为imageView

相关问题