当前位置:我的异常网» Java Web开发 » Android发送短信与邮件,该如何处理
Android发送短信与邮件,该如何处理
www.myexceptions.net 网友分享于:2013-07-29 浏览:7次
Android发送短信与邮件
Android发送短信与邮件
Java代码
发送短信:
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(address, null, message, null, null);
显示写短信界面:
Uri smsToUri = Uri.parse("smsto://10086");
Intent mIntent = new Intent( android.content.Intent.ACTION_SENDTO, smsToUri );
startActivity( mIntent );
发送电子邮件:
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_, address);
i.putExtra(Intent.EXTRA_SUBJECT, filename);
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filename)); ;
i.setType("text/csv");
startActivity(Intent.createChooser(i, " File"));
Android API - SMS handling
Many new application will use SMS as data delivery platform. Reality shows, on-demand movies etc request users to send predefined formatted SMS. Similarly some applications are coming up which sends data to user using SMS. Let’s see how such an application can be built using Android platform.
Android API support developing applications that can send and receive SMS messages. The android emulator does not support sending of the SMS currently. But the emulator can receive SMS. Lets explore the android SMS support and develop a small program that listens to the SMSes received on the device (on emulator) and will show that message as notification.
The event handling on Android is done with the help of intents and intent receivers. The intents announce (or broadcast) the event and intent receivers respond to the event. Intent receivers act as the event handlers.
Let’s define an intent receiver that can handle the SMS received event:
Code: Select all
package com.wissen.sms.receiver;
/**
* The class is called when SMS is received.
*/
public class SMSReceiver extends BroadcastReceiver {
@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 ). ‘Emulator Control’ window can be used to send SMS message (an incoming number has to be provided which can be anything).
------解决方案--------------------
文章评论