标签:国际 ++ lte 打开 手动 prot cti err state
效果:点击发送短信开始发送短信
收到短信时将短信的内容显示出来
代码如下:
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" />
我们的短信功能用到了SmsManager和SmsMessage两个主要类。
当收不到短信时候,手动在应用管理中将应用的短信收发权限打开
//Java /** * Created by Linda on 16/5/14. * 短信的收发 * 根绝国际标准 每条短信的长度不超过160 个字符 * 如果短信长度过长,可以调用sendMultipartTextMessage()分隔成多条短信来发送 */ public class MesaageActivity extends Activity { private TextView sender; private TextView content; private EditText to;//接收短信的手机号码 private EditText msgInput;//发送信息的内容 private Button send; private IntentFilter receiveFilter;//短信接收 private MessageReceiver messageReceiver; private IntentFilter sendFilter;//短信发送 private SendStatusReceiver sendStatusReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.message); sender = (TextView) findViewById(R.id.sender); content = (TextView) findViewById(R.id.content); to = (EditText) findViewById(R.id.to); msgInput = (EditText) findViewById(R.id.msg_input); send = (Button) findViewById(R.id.send); // 注册短信接收的广播 receiveFilter = new IntentFilter(); receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); receiveFilter.setPriority(100); messageReceiver = new MessageReceiver(); registerReceiver(messageReceiver, receiveFilter); // 注册监听短信发送的广播 sendFilter = new IntentFilter(); sendFilter.addAction("SENT_SMS_ACTION"); sendStatusReceiver = new SendStatusReceiver(); registerReceiver(sendStatusReceiver, sendFilter); // 点击按钮发送短信 send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SmsManager smsManager = SmsManager.getDefault(); Intent sentIntent = new Intent("SENT_SMS_ACTION"); PendingIntent pi = PendingIntent.getBroadcast( MesaageActivity.this, 0, sentIntent, 0); //根据号码和内容发送短信(需要权限) smsManager.sendTextMessage(to.getText().toString(), null, msgInput.getText().toString(), pi, null); } }); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(messageReceiver); unregisterReceiver(sendStatusReceiver); } //短信接收的广播 class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Object[] pdus = (Object[]) bundle.get("pdus");//获取短信数组 SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);//将pdu字符数组转换成SmsMessage对象 } String address = messages[0].getOriginatingAddress();//获取短信发送方的号码 String fullMessage = ""; for (SmsMessage message : messages) { fullMessage += message.getMessageBody();//获取短信的内容 } sender.setText(address); content.setText(fullMessage); abortBroadcast(); } } //监听广播是否发送成功的广播 class SendStatusReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (getResultCode() == RESULT_OK) { Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(context, "Send failed", Toast.LENGTH_LONG) .show(); } } } }
标签:国际 ++ lte 打开 手动 prot cti err state
原文地址:http://www.cnblogs.com/eniac1946/p/7591461.html