posts - 93,  comments - 2,  trackbacks - 0

          1、PendingIntent作用

          根據(jù)字面意思就知道是延遲的intent,主要用來在某個(gè)事件完成后執(zhí)行特定的Action。PendingIntent包含了Intent及Context,所以就算Intent所屬程序結(jié)束,PendingIntent依然有效,可以在其他程序中使用。
          常用在通知欄及短信發(fā)送系統(tǒng)中。

          PendingIntent一般作為參數(shù)傳給某個(gè)實(shí)例,在該實(shí)例完成某個(gè)操作后自動(dòng)執(zhí)行PendingIntent上的Action,也可以通過PendingIntent的send函數(shù)手動(dòng)執(zhí)行,并可以在send函數(shù)中設(shè)置OnFinished表示send成功后執(zhí)行的動(dòng)作。


          2.舉例(通知欄應(yīng)用)
              界面A 定義一個(gè)notification    
              NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
              int icon = R.drawable.notification;
              long when = System.currentTimeMillis()+2000;
              Notification n = new Notification(icon,"標(biāo)題",when);
              n.defaults = Notification.DEFAULT_SOUND;
              n.flags |= Notification.FLAG_AUTO_CANCEL;
              Intent intent = new Intent(this,B.class);
              PendingIntent pi = new PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
              n.setLatesEventInfo(this,"通知欄demo提醒title","通知欄demo提醒text",pi);
              nm.notify(0,n);

              B.界面Intent intent = getIntent();
            String title = intent.getStringExtra("title");

              效果,當(dāng)A界面顯示,生成一個(gè)按鈕,點(diǎn)擊該按鈕生成如上所示的通知欄,點(diǎn)擊通知欄,則顯示B界面,參數(shù)title為所顯示的值。

          3、Intent和PendingIntent的區(qū)別

          a. Intent是立即使用的,而PendingIntent可以等到事件發(fā)生后觸發(fā),PendingIntent可以cancel
          b. Intent在程序結(jié)束后即終止,而PendingIntent在程序結(jié)束后依然有效
          c. PendingIntent自帶Context,而Intent需要在某個(gè)Context內(nèi)運(yùn)行
          d. Intent在原task中運(yùn)行,PendingIntent在新的task中運(yùn)行


          posted on 2013-05-22 15:34 Terry Zou 閱讀(250) 評論(0)  編輯  收藏 所屬分類: Android
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 北宁市| 德清县| 简阳市| 嘉禾县| 安阳市| 锡林郭勒盟| 塔河县| 西盟| 凯里市| 峨山| 天台县| 平原县| 井陉县| 油尖旺区| 隆子县| 孟连| 黑水县| 新乡县| 新蔡县| 南陵县| 五台县| 通江县| 鞍山市| 黑龙江省| 磴口县| 新疆| 九寨沟县| 改则县| 天门市| 隆安县| 北票市| 佛山市| 龙泉市| 巫溪县| 忻州市| 会昌县| 沾化县| 北流市| 洪雅县| 甘洛县| 时尚|