我在讀android短信。在那裏,我得到了代碼,但我很困惑,或者沒有真正理解它。如何理解短信發送代碼?
public class SMSActivity extends Activity
{
String SENT = 「SMS_SENT」;
String DELIVERED = 「SMS_DELIVERED」;
PendingIntent sentPI, deliveredPI;
BroadcastReceiver smsSentReceiver, smsDeliveredReceiver
@Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
}
@Override
public void onResume()
{
super.onResume();
smsSentReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK: Toast.makeText(getBaseContext(), 「SMS sent」, Toast.LENGTH_SHORT).show(); break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), 「Genericfailure」,Toast.LENGTH_SHORT).show(); break;
}}
};
smsDeliveredReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), 「SMS delivered」, Toast.LENGTH_SHORT).show(); break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), 「SMS not delivered」, Toast.LENGTH_SHORT).show(); break;
}
}
registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));
registerReceiver(smsSentReceiver, new IntentFilter(SENT)); }
//button click event method
public void onClick(View v)
{ sendSMS(「5556」, 「Hello my friends!」); }
private void sendSMS(String phoneNumber, String message)
{ SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}
這裏我有2層未決的意圖:sentPI & deliveredPI,其中我使用的sendTextMessage內();這意味着我正在向smsManager類發送掛起的意圖。
而且我已經註冊了2個廣播接收器,它們將通過匹配SMS_SENT & SMS_DELIVERED的意圖來接收。 在書中它說SMS_SENT & SMS_DELIVERED將由SmsManager觸發,當消息被髮送或傳遞時。它意味着生病獲取SMS_SENT的意圖消息& SMS_DELIVERED from SmsManger?或者它是我發送sendTextMessage的意圖? 困惑..... SomeOne PLZ幫助我? PLZ讓我瞭解週期?
這意味着如果我給SMS_Sent_Success,比SsmManager會廣播它而不是SMS_SENT?並在registerReciever()我也必須使用SMS_sent_success字符串(意圖)?並且SMS_SENT不是真正的Smsmanager自己的字符串/意圖? – shaon007
我編輯了我的答案。希望能幫助到你。 – Jim