发送短信

SMSActivity.java

package src.sms;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSActivity extends Activity {

private String m_currentTelNumber;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//设置短信UI
//设置拨号按钮的事件监听器
Button smsButton = (Button) findViewById(R.id.smsButton);
smsButton.setOnClickListener(new SmsButtonListener());
}

/**
* 使用SmsMessage来发送短信
*/
private class SmsButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
m_currentTelNumber = "09xxxxxxxxxxx";

EditText messageBody = (EditText) findViewById(R.id.sms_message);
//调用一个短信管理器
SmsManager smsManager = SmsManager.getDefault();
//使用短信管理传送信息
smsManager.sendTextMessage(m_currentTelNumber, null, messageBody.getText().toString(), null, null);
//调用一个Toast信息确定函数有被执行
String toastMessage = String.format("To:%1$s\nMessage Body:%2$s", m_currentTelNumber,messageBody.getText().toString());
Toast.makeText(SMSActivity.this, toastMessage, Toast.LENGTH_LONG).show();
}

}

}

 

广播机制,监控短信接收

SmsReceiver.java

package src.sms.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.widget.Toast;

/**
* 监控短信接收 SmsReceiver实现BroadcastReceiver,
* 当Android系统广播SMS_RECEIVED时就接收,并产生一个信息确认有接收到
*/
public class SmsReceiver extends BroadcastReceiver {
// 设置ACTION字符串来比对intent是否为接收短信
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent) {
// 只有属于接收短信的内容才需要处理
if (intent != null && intent.getAction() != null
&& (ACTION.compareToIgnoreCase(intent.getAction()) == 0)) {
// 从Intent 中读取pdu的资料
Object[] pduData = (Object[]) intent.getExtras().get("pdus");
// 依照pduData的数组大小来添加一个短信的数组
SmsMessage[] smsArray = new SmsMessage[pduData.length];
// 将数据写入短信数组并且列出内容
for (int i = 0; i < pduData.length; i++) {
smsArray[i] = SmsMessage.createFromPdu((byte[]) pduData[i]);
String toastMessage = String
.format("Message clip #%3$d, \n From : %2$s, \n Message Body: %1$s",
smsArray[i].getMessageBody(),
smsArray[i].getOriginatingAddress(),
i);
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
}
}
}
}

 

注册广播机制和添加权限

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="src.sms"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SMSActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receiver.SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>

</manifest>

你可能感兴趣的:(短信)