java 发短信 发邮件,Android发送短信与邮件,该如何处理

当前位置:我的异常网» 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).

------解决方案--------------------

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值