Android收发短信

eniac1946 · · 332 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

效果:点击发送短信开始发送短信

           收到短信时将短信的内容显示出来

代码如下:

一、权限声明

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

读写,系统的短信库存在data/data/com.android.providers.telephony/databases/mmssms.db  但真机不可读

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_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 API>23,需要改写

 

//接收类另一种写法

public class MessageDemo extends BroadcastReceiver {
    private static final String strACT = "android.provider.Telephony.SMS_RECEIVED";

public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(strACT)) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msg = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage currMsg : msg) {
sb.append("From:");
sb.append(currMsg.getDisplayOriginatingAddress());
sb.append("\nMessage:");
sb.append(currMsg.getDisplayMessageBody());
}
}
}

}
}

三、模拟短信发送

Android Emulator模拟发送短信

 接下来发短信打电话,设置gps什么的就选择相应的标签设置就好了

 

  

本文来自:博客园

感谢作者:eniac1946

查看原文:Android收发短信

332 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet