Android短信收发

jopen 10年前

短信发送,有些机型可以在后台向指定的手机发送短信而不会有提示,有些机型可能会提醒用户让用户决定到底发不发送,我想这应该是出于安全的考虑

实现短信的发送,我们可以通过注册receiver得知短信发送是否成功,对方是否接受到了

    /**        * 发送短信        * @param friend_num        * @param smsMsg        */        private void sendSMS(String friend_num, String smsMsg) {                    String SENT_SMS_ACTION = "SENT_SMS_ACTION";            Intent sentIntent = new Intent(SENT_SMS_ACTION);            PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,                    sentIntent, 0);            // register the Broadcast Receivers            this.registerReceiver(new BroadcastReceiver() {                @Override                public void onReceive(Context _context, Intent _intent) {                    switch (getResultCode()) {                    case Activity.RESULT_OK:                        //Toast.makeText(this, "短信发送成功", Toast.LENGTH_SHORT)                        //      .show();                                                break;                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:                        break;                    case SmsManager.RESULT_ERROR_RADIO_OFF:                        break;                    case SmsManager.RESULT_ERROR_NULL_PDU:                        break;                    }                }            }, new IntentFilter(SENT_SMS_ACTION));                    String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";            // create the deilverIntent parameter            Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);            PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0,                    deliverIntent, 0);            this.registerReceiver(new BroadcastReceiver() {                @Override                public void onReceive(Context _context, Intent _intent) {                    //Toast.makeText(this, "收信人已经成功接收", Toast.LENGTH_SHORT).show();                }            }, new IntentFilter(DELIVERED_SMS_ACTION));                    // 直接调用短信接口发短信            SmsManager smsManager = SmsManager.getDefault();            List<String> divideContents = smsManager.divideMessage(smsMsg);            //可能短信的内容过长,可以自动分两封发,如果信息中包含一些连贯的东西,还是手动拆分比较好            for (String text : divideContents) {                smsManager.sendTextMessage(friend_num, null, text, sentPI,                        deliverPI);            }                }  
</div> </div>

短信的拦截,这个功能有点不可靠,有些机型压根就拦截不到短信(我的M1S,360、QQ手机管家都拦截不到)手机里面内置了一个短信拦截的软件,优先级别是最高的,但大多数手机还是能够拦截的到的,但也存在一个问题,由于短信接收触发的广播是有序的广播,是一级级往后传递的,不管哪一级都可以切断,后续的就接收不到了,具有相同功能的应用之间的优先级问题,手机qq管家这方面做的比360好,优先级更高,我们自己编写的短信拦截软件也可以把360的干掉,但 qq的就难办了...