这是我已经实现的,它的工作方式正是我想要的。
输入电话号码和短信后调用此方法。
private static final int MAX_SMS_MESSAGE_LENGTH = 160;
private static final int SMS_PORT = 8901;
private static final String SMS_DELIVERED = "SMS_DELIVERED";
private static final String SMS_SENT = "SMS_SENT";
private void sendSms(String phonenumber,String message) {
SmsManager manager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
byte[] data = new byte[message.length()];
for(int index=0; index<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
{
data[index] = (byte)message.charAt(index);
}
manager.sendDataMessage(phonenumber, null, (short) SMS_PORT, data,piSend, piDelivered);
}
private BroadcastReceiver sendreceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String info = "Send information: ";
switch(getResultCode())
{
case Activity.RESULT_OK: info += "send successful"; break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: info += "send failed, generic failure"; break;
case SmsManager.RESULT_ERROR_NO_SERVICE: info += "send failed, no service"; break;
case SmsManager.RESULT_ERROR_NULL_PDU: info += "send failed, null pdu"; break;
case SmsManager.RESULT_ERROR_RADIO_OFF: info += "send failed, radio is off"; break;
}
Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
}
};
private BroadcastReceiver deliveredreceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String info = "Delivery information: ";
switch(getResultCode())
{
case Activity.RESULT_OK: info += "delivered"; break;
case Activity.RESULT_CANCELED: info += "not delivered"; break;
}
Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
}
};
您的消息接收者应如下所示:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
public class MySMSReceiver extends BroadcastReceiver {
String action,from,message;
@Override
public void onReceive(Context context, Intent intent) {
action=intent.getAction();
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if(null != bundle)
{
String info = "Binary SMS from ";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
byte[] data = null;
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
info += msgs[i].getOriginatingAddress();
info += "\n*****BINARY MESSAGE*****\n";
from= msgs[i].getOriginatingAddress();
data = msgs[i].getUserData();
for(int index=0; index<data.length; ++index) {
info += Character.toString((char)data[index]);
message += Character.toString((char)data[index]);
}
}
}
Intent showMessage=new Intent(context, AlertMessage.class);
showMessage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
showMessage.putExtra("from", from);
showMessage.putExtra("message", message);
context.startActivity(showMessage);
}
}
我创建了一个简单的活动 AlertMessage.java 来显示收到的消息。
我在 Manifest 中注册广播接收器的方式:
<receiver android:name=".MySMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>
这里提到的端口必须与我们在方法 sendSMS() 中指定的用于发送消息的端口相同。
更新 :
工作项目的 Github 存储库
https://github.com/pyus-13/MySMSSender