轉自:http://blog.csdn.net/xuzhuang2008/archive/2011/04/13/6321477.aspx
Java代碼
1.Notification n = new Notification(R.drawable.face_1, "Service啟動", System.currentTimeMillis());
2.PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TServiceHolder.class), 0);
3.n.setLatestEventInfo(this, "任務標題", "任務內容", contentIntent);
4.nManager.notify(NOTIFICATION_ID, n); // 工作列啟動
PendingIntent和Intent的區別:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.
1. GSM網路中android傳送簡訊樣本
(1)代碼節選
Java代碼
1.String msg ="你好,美女";
2.String number = "135****6784";
3.SmsManager sms = SmsManager.getDefault();
4.
5.PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
6.sms.sendTextMessage(number, null, msg, pi, null);
7.Toast.makeText(SmsActivity.this,"發送成功",Toast.LENGHT_LONG).show();
(2)代碼解釋
PendingIntent就是一個Intent的描述,我們可以把這個描述交給別的程式,別的程式根據這個描述在後面的別的時間做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相當於PendingIntent代表了Intent)。本例中別的程式就是傳送簡訊的程式,簡訊發送成功後要把intent廣播出去 。
函數SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中參數解釋:
1)PendingIntent sentIntent:當簡訊發出時,成功的話sendIntent會把其內部的描述的intent廣播出去,否則產生錯誤碼並通過android.app.PendingIntent.OnFinished進行回調,這個參數最好不為空白,否則會存在資源浪費的潛在問題;
2)PendingIntent deliveryIntent:是當訊息已經傳遞給收信人後所進行的PendingIntent廣播。
查看PendingIntent 類可以看到許多的Send函數,就是PendingIntent在進行被賦予的相關的操作。
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/xuzhuang2008/archive/2011/04/13/6321477.aspx