日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android PendingIntent参数详细解析

發布時間:2025/6/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android PendingIntent参数详细解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PendingIntent介紹

PendingIntent可以看作是對Intent的一個封裝,但它不是立刻執行某個行為,而是滿足某些條件或觸發某些事件后才執行指定的行為。

PendingIntent的獲取

PendingIntent獲取有三種方式:通過Activity,Service,BroadcastReceiver獲取.

1. 你可以通過getActivity(Context context, int requestCode, Intent intent, int flags)系列方法從系統 取得一個用于啟動一個Activity的PendingIntent對象.

2.可以通過getService(Context context, int requestCode, Intent intent, int flags)方法從系統取得一個 用于啟動一個Service的PendingIntent對象.

3.可以通過getBroadcast(Context context, int requestCode, Intent intent, int flags)方法從系統取得一個用于向BroadcastReceiver的發送廣播的PendingIntent對象.

PendingIntent的參數說明

拿第三種方式,廣播的形式說明下

PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0,sIntent, 0);

第一個參數是上下文.

第二個參數是每次requestcode不同,就能產生多個Pendingintent.

第三個參數是用來存儲信息.

第四個參數是對不同操作作標識.

getBroadcast(Context context, int requestCode, Intent intent, int flags)中的flags有幾種狀態:
1.FLAG_CANCEL_CURRENT:如果AlarmManager管理的PendingIntent已經存在,那么將會取消當前的PendingIntent,從而創建一個新的PendingIntent.

2.FLAG_UPDATE_CURRENT:如果AlarmManager管理的PendingIntent已經存在,讓新的Intent更新之前Intent對象數據,
例如更新Intent中的Extras,另外,我們也可以在PendingIntent的原進程中調用PendingIntent的cancel ()把其從系統中移除掉

3.FLAG_NO_CREATE:如果AlarmManager管理的PendingIntent已經存在,那么將不進行任何操作,直接返回已經.

4.FLAG_ONE_SHOT:該PendingIntent只作用一次.在該PendingIntent對象通過send()方法觸發過后,
PendingIntent將自動調用cancel()進行銷毀,那么如果你再調用send()方法的話,系統將會返回一個SendIntentException.


PendingIntent舉例

發送短信:

private final static String SEND_ACTION = "send"; private final static String DELIVERED_ACTION = "delivered"; private void sendSms(String receiver, String text) { SmsManager s = SmsManager.getDefault(); PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SEND_ACTION), PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED_ACTION), PendingIntent.FLAG_CANCEL_CURRENT); // 發送完成 registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "Send Success!", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), "Send Failed because generic failure cause.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), "Send Failed because service is currently unavailable.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), "Send Failed because no pdu provided.", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), "Send Failed because radio was explicitly turned off.", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getBaseContext(), "Send Failed.", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(SEND_ACTION)); // 對方接受完成 registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "Delivered Success!", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getBaseContext(), "Delivered Failed!", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED_ACTION)); // 發送短信,sentPI和deliveredPI將分別在短信發送成功和對方接受成功時被廣播 s.sendTextMessage(receiver, null, text, sentPI, deliveredPI); }

這里特別說明下:群發短信的時候,要保持每次發送的短信內容都不相同.可以將PendingIntent的第二個參數設置成不同的值,第四個參數設置成FLAG_ONE_SHOT.

這樣就可以解決群發短信的兩個問題:

1.群發短信時,短信內容取到的值是最后一條短信的內容.

2.群發短信時,短信內容總是不更新, 一成不變的問題.

總結

以上是生活随笔為你收集整理的android PendingIntent参数详细解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。