三星手机 Notification PendingIntent

2020-07-27  本文已影响0人  cover_tech

三星手机 Notification PendingIntent 传值问题

PendingIntent简单介绍

通知栏跳转

//获取一个用于启动 Activity 的 PendingIntent 对象
public static PendingIntent getActivity(Context context, int requestCode,Intent intent, @Flags int flags)
//获取一个用于启动 Activity 的 PendingIntent 对象
public static PendingIntent getService(Context context, int requestCode,Intent intent, @Flags int flags)
//获取一个用于启动 Activity 的 PendingIntent 对象
public static PendingIntent getBroadcast(Context context, int requestCode,Intent intent, @Flags int flags)

参数说明

PendingIntent的构建中的flags FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT

API29官方文档:

 Flag indicating that this PendingIntent can be used only once.
 For use with {@link #getActivity}, {@link #getBroadcast}, and
 {@link #getService}. <p>If set, after
 {@link #send()} is called on it, it will be automatically
 canceled for you and any future attempt to send through it will fail.

意思就是:利用 FLAG_ONE_SHOT获取的PendingIntent只能使用一次,即使再次利用上面三个方法重新获取,再使用PendingIntent也将失败

API29官方文档:

Flag indicating that if the described PendingIntent does not
already exist, then simply return null instead of creating it.
For use with {@link #getActivity}, {@link #getBroadcast}, and
{@link #getService}.

意思就是:利用FLAG_NO_CREAT获取的PendingIntent,若描述的Intent不存在则返回NULL值.

API29官方文档:

Flag indicating that if the described PendingIntent already exists,
the current one should be canceled before generating a new one.
For use with {@link #getActivity}, {@link #getBroadcast}, and
{@link #getService}. <p>You can use
this to retrieve a new PendingIntent when you are only changing the
extra data in the Intent; by canceling the previous pending intent,
this ensures that only entities given the new data will be able to
launch it.  If this assurance is not an issue, consider
{@link #FLAG_UPDATE_CURRENT}.

意思就是:如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的。你可用使用它去检索新的Intent,如果你只是想改变Intent中的额外数据的话。通过取消先前的Intent,可用确保只有最新的实体可用启动它。如果这一保证不是问题,考虑flag_update_current。

API29官方文档:

Flag indicating that if the described PendingIntent already exists,
then keep it but replace its extra data with what is in this new
Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
{@link #getService}. <p>This can be used if you are creating intents where only the
extras change, and don't care that any entities that received your
previous PendingIntent will be able to launch it with your new
extras even if they are not explicitly given to it.

意思就是:最经常使用的是FLAG_UPDATE_CURRENT,因为描述的Intent有 更新的时候需要用到这个flag去更新你的描述,否则组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras。

具体使用哪种flag可以根据具体需求和业务情况。这里我们项目使用的是FLAG_UPDATE_CURRENT

项目中遇到的问题

在其他手机上,这个Activity里面都能接收到参数,但是测试发现只有在三星a8s这款机型上接收不到参数

部分代码 PendingIntent跳转相关代码

 Intent resultIntent = new Intent(context, NewsDetailActivity.class);
//listItemEntity是一个Serializable序列化的实体类
 resultIntent.putExtra(NewsDetailActivity.ARG_DATA, listItemEntity);
 resultIntent.putExtra(NewsDetailActivity.ARG_FROM_PUSH, true);
pendingIntent = PendingIntent.getActivity(context, (int) itemEntity.getDetail_id(), resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

查找了很多网上解决方案:

1,Intent添加flags的形式

resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED| Intent.FLAG_ACTIVITY_NEW_TASK)

2,Intent添加Action的形式

resultIntent.setAction("action")

但是结果都是不行

先采用String的形式随便传递一个参数,发现能够接收到参数(到这儿已经有解决思路了,而且确实也是这样解决的)

然后用Parcelable 序列化传值还是不行,最后的解决方案是通过String的形式传递,然后解决的

可以通过PendingIntent.getBroadcast 发送广播,然后启动Activity传递参数(还没有尝试)
可以通过PendingIntent.getService 启动服务,然后启动Activity传递参数(还没有尝试)

本文作者:银进(silver)
本文为原创作品,未经允许不得转载。

上一篇 下一篇

猜你喜欢

热点阅读