本例介绍Android收发短信,可以使用实际的设备,如果使用模拟器,可以启动两个模拟器来测试,可以参见Android简明开发教程二十:Broadcast Receiver 短信触发示例
本例还使用了TTS来读出收到的短信内容SmsReceivedDialog 中使用TextToSpeech 类来读出文字,TextToSpeech的用法可以参见Android ApiDemos示例解析(45):App->Text-To-Speech
本例如果Enable SMS broadcast receiver ,允许手机在收到短信时自动启动Acitivity(由SmsMessageReceiver触发SmsReceivedDialog),使用Broadcast receiver 也请参见Android简明开发教程二十:Broadcast Receiver 短信触发示例 ,不过本例是通过PackageManage动态注册Broadcast Receiver的方法:
final PackageManager pm = this.getPackageManager(); final ComponentName componentName = new ComponentName("com.example.android.apis", "com.example.android.apis.os.SmsMessageReceiver"); enableCheckBox .setChecked(pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED); enableCheckBox.setOnCheckedChangeListener( new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Log.d(TAG, (isChecked ? "Enabling" : "Disabling") + " SMS receiver"); pm.setComponentEnabledSetting(componentName, isChecked ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); } });
下面来介绍收发短信SMS,短信可以分两种一种为Text Message 主要给人来阅读,另外一种为Data Message (binary)可以发个另外的应用。
在Android在收发SMS是通过SmsManager来完成。 可以通过SmsManager.getDefault()来取得一个SmsManager对象
SmsManager sms = SmsManager.getDefault();
此外需要在AndroidManifest.xml在添加使用SMS对应的权限:
<uses-permission android:name=”android.permission.SEND_SMS” />
<uses-permission android:name=”android.permission.RECEIVE_SMS” />
简单的发送一条短信:
String sendTo="123456789"; String myMessage="Greetings from Guidebee"; smsManager.sendText(sendTo,null,myMessage,null,null);
public final void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
本例定义了sendIntent来跟踪SMS发送的状态:
sms.sendTextMessage(recipient, null, message, PendingIntent.getBroadcast( SmsMessagingDemo.this, 0, new Intent(ACTION_SMS_SENT), 0), null); ... // Register broadcast receivers for SMS sent and delivered intents registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String message = null; boolean error = true; switch (getResultCode()) { case Activity.RESULT_OK: message = "Message sent!"; error = false; break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: message = "Error."; break; case SmsManager.RESULT_ERROR_NO_SERVICE: message = "Error: No service."; break; case SmsManager.RESULT_ERROR_NULL_PDU: message = "Error: Null PDU."; break; case SmsManager.RESULT_ERROR_RADIO_OFF: message = "Error: Radio off."; break; } recipientTextEdit.setEnabled(true); contentTextEdit.setEnabled(true); contentTextEdit.setText(""); statusView.setText(message); statusView.setTextColor(error ? Color.RED : Color.GREEN); } }, new IntentFilter(ACTION_SMS_SENT)); }
类似的
public final void sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent)可以用来发送Data message.