首页 > 其他 > 详细

adnroid 监听收到的短信并根据短信内容进行回复短信

时间:2014-02-17 19:19:21      阅读:470      评论:0      收藏:0      [点我收藏+]

定义一个广播接收器

bubuko.com,布布扣
public class SMSReceiver extends BroadcastReceiver {
    private SmsManager smsManager;

    @Override
    public void onReceive(Context arg0, Intent intent) {
        
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            
            SharedPreferences sp = arg0.getSharedPreferences("MSG_SEND_RESULT", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            

            smsManager = SmsManager.getDefault();
            
            Object[] pdus=(Object[])intent.getExtras().get("pdus");
            
            SmsMessage[] message=new SmsMessage[pdus.length];
            
            for(int i=0;i<pdus.length;i++){
                message[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
                
                String msgAddr = message[i].getDisplayOriginatingAddress();
                String msgContent = message[i].getDisplayMessageBody();
                
                //自动回复短信
                String content = "";
                int count = 0;
                if(msgContent.trim().equals("1")){//android
                    content = "android";
                    smsManager.sendTextMessage(msgAddr, null, content, null, null);
                    count = sp.getInt("android", 0)+1;
                    editor.putInt("android", count);
                    editor.apply();
                }else if(msgContent.trim().equals("2")){//ios
                    content = "ios";
                    smsManager.sendTextMessage(msgAddr, null, content, null, null);
                    count = sp.getInt("iphone", 0)+1;
                    editor.putInt("iphone", count);
                    editor.apply();
                }
            }
        }
    }
}
bubuko.com,布布扣

添加权限:

bubuko.com,布布扣
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
bubuko.com,布布扣

注册receiver

bubuko.com,布布扣
<receiver android:name="com.auto.SMSReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
</receiver>
bubuko.com,布布扣

adnroid 监听收到的短信并根据短信内容进行回复短信

原文:http://www.cnblogs.com/yshyee/p/3552494.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!