三星手機(jī) Notification PendingIntent 傳值問(wèn)題
PendingIntent簡(jiǎn)單介紹
通知欄跳轉(zhuǎn)
- 啟動(dòng)Activity
//獲取一個(gè)用于啟動(dòng) Activity 的 PendingIntent 對(duì)象
public static PendingIntent getActivity(Context context, int requestCode,Intent intent, @Flags int flags)
- 啟動(dòng)Service
//獲取一個(gè)用于啟動(dòng) Activity 的 PendingIntent 對(duì)象
public static PendingIntent getService(Context context, int requestCode,Intent intent, @Flags int flags)
- 發(fā)送廣播
//獲取一個(gè)用于啟動(dòng) Activity 的 PendingIntent 對(duì)象
public static PendingIntent getBroadcast(Context context, int requestCode,Intent intent, @Flags int flags)
參數(shù)說(shuō)明
context 上下文
requestCode 標(biāo)志位-標(biāo)記不同的pendingIntent
intent 意圖 用于跳轉(zhuǎn)activity 、啟動(dòng)Service 聪富、發(fā)送廣播來(lái)構(gòu)建PendingIntent的意圖
flags PendingIntent的行為
PendingIntent的構(gòu)建中的flags FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT
- FLAG_ONE_SHOT
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只能使用一次,即使再次利用上面三個(gè)方法重新獲取掘宪,再使用PendingIntent也將失敗
- FLAG_NO_CREATE
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值.
- FLAG_CANCEL_CURRENT
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已經(jīng)存在歇盼,則在產(chǎn)生新的Intent之前會(huì)先取消掉當(dāng)前的。你可用使用它去檢索新的Intent评抚,如果你只是想改變Intent中的額外數(shù)據(jù)的話豹缀。通過(guò)取消先前的Intent,可用確保只有最新的實(shí)體可用啟動(dòng)它盈咳。如果這一保證不是問(wèn)題,考慮flag_update_current边翼。
- 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.
意思就是:最經(jīng)常使用的是FLAG_UPDATE_CURRENT鱼响,因?yàn)槊枋龅腎ntent有 更新的時(shí)候需要用到這個(gè)flag去更新你的描述,否則組件在下次事件發(fā)生或時(shí)間到達(dá)的時(shí)候extras永遠(yuǎn)是第一次Intent的extras组底。
具體使用哪種flag可以根據(jù)具體需求和業(yè)務(wù)情況丈积。這里我們項(xiàng)目使用的是FLAG_UPDATE_CURRENT
項(xiàng)目中遇到的問(wèn)題
- 我們?cè)谑褂肞endingIntent跳轉(zhuǎn)到相應(yīng)的Activity并且傳遞參數(shù)
在其他手機(jī)上,這個(gè)Activity里面都能接收到參數(shù)债鸡,但是測(cè)試發(fā)現(xiàn)只有在三星a8s這款機(jī)型上接收不到參數(shù)
部分代碼 PendingIntent跳轉(zhuǎn)相關(guān)代碼
Intent resultIntent = new Intent(context, NewsDetailActivity.class);
//listItemEntity是一個(gè)Serializable序列化的實(shí)體類
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);
查找了很多網(wǎng)上解決方案:
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")
但是結(jié)果都是不行
- 解決過(guò)程
先采用String的形式隨便傳遞一個(gè)參數(shù)厌均,發(fā)現(xiàn)能夠接收到參數(shù)(到這兒已經(jīng)有解決思路了,而且確實(shí)也是這樣解決的)
然后用Parcelable 序列化傳值還是不行唬滑,最后的解決方案是通過(guò)String的形式傳遞,然后解決的
- 額外思路
可以通過(guò)PendingIntent.getBroadcast 發(fā)送廣播棺弊,然后啟動(dòng)Activity傳遞參數(shù)(還沒(méi)有嘗試)
可以通過(guò)PendingIntent.getService 啟動(dòng)服務(wù)晶密,然后啟動(dòng)Activity傳遞參數(shù)(還沒(méi)有嘗試)
本文作者:銀進(jìn)(silver)
本文為原創(chuàng)作品,未經(jīng)允許不得轉(zhuǎn)載模她。