开发学院手机开发Android 开发 Android发送短信与邮件 阅读

Android发送短信与邮件

 2010-11-05 00:54:29 来源:WEB开发网   
核心提示:@Overridepublic void onReceive(Context context, Intent intent) {// TODO}}We need to configure this intent receiver to receive SMS receive event. For SMS receive

@Override

public void onReceive(Context context, Intent intent) {

// TODO

}

}

We need to configure this intent receiver to receive SMS receive event. For SMS receive event android has defined an intent as ‘ android.provider.Telephony.SMS_RECEIVED ‘. The receiver can be configured in AndroidManifest.xml as follows:

Code: Select all

To receive SMS, application also needs to specify permission for receiving SMS. The permission can be set in AndroidManifest.xml as follows:

Code: Select all

Now our intent receiver is all set to be called when the android device will receive SMS. Now we only need to retrieve the received SMS and show the SMS text in a notification.

Here is the code of intent receiver that will read the SMS from intent received and show the first message (pdu).

Code: Select all

public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();

Object messages[] = (Object[]) bundle.get(”pdus”);

SmsMessage smsMessage[] = new SmsMessage[messages.length];

for (int n = 0; n < messages.length; n++) {

smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);

}

// show first message

Toast toast = Toast.makeText(context,

“Received SMS: ” + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);

toast.show();

}

The SMS received by the Android device is in the form of pdus (protocol description unit). Class SmsMessage, defined in android.telephony.gsm package, can store information about the SMS. The class can also be used to create SmsMessage object from received pdus. Toast widget is used to show the SMS body as an notification.

Running the Program:

Only remaining thing now is running the application and sending the SMS message to the emulator. An SMS message can be sent to the emulator in the DDMS eclipse perspective (Dalvik Debug Monitor Service). ‘Emulator Control’ window can be used to send SMS message (an incoming number has to be provided which can be anything).

上一页  1 2 

Tags:Android 发送 短信

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接