码迷,mamicode.com
首页 > 移动开发 > 详细

Android收发短信

时间:2017-09-25 14:48:06      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:国际   ++   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();  
            }  
        }  
  
    }  
  
}  

  

Android收发短信

标签:国际   ++   lte   打开   手动   prot   cti   err   state   

原文地址:http://www.cnblogs.com/eniac1946/p/7591461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!