Android应用发送短信的实现

实现Android应用的发送消息


实现两个安卓机之间发送短消息:
新建一个Android项目:
在MainActivity中添加一个EditText,id=et_content,用来输入想要发送的短信内容
然后添加一个Button按钮,it=btn_send,点击后发送短消息
然后在MainActivity.java中添加如下代码:

    EditText etContent; //声明输入框
    Button btnSend; //声明发送按钮
    private MyReciver receiver;//声明一个广播接收器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etContent=(EditText) findViewById(R.id.et_content);
        btnSend=(Button) findViewById(R.id.bt_send);
        //在发送按钮上添加一个监听器,点击发送后会执行onClick方法
        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            //在onClick方法中执行SendSms()方法
                sendSMS();
            }
        });
    }
    protected void sendSMS() {



//通过隐式发送intent实现
----------------

        //启动系统自带的action:ACTION_SENDTO
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        //发送给号码为15555215554的设备
        Uri data = Uri.parse("smsto:"+15555215554);
        intent.setData(data);
        //"sms_body"是指系统数据表sms中的body字段,存放的是短信内容
        intent.putExtra("sms_body", etContent.getText().toString());
        startActivity(intent);



//另外一种通过发送隐式intent的方式来实现
------------------------

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setType("vnd.android-dir/mms-sms");
        //address为数据表中存放电话号码的字段
        intent.putExtra("address", "15555215554");
        intent.putExtra("sms_body", etContent.getText().toString());
        startActivity(intent);
        //以上两种方法,除了电话号码外其他都为固定用法,记住就好



//利用代码发送短信, 但是短信不会被写入系统数据表
--------------------------
//利用代码发送短信,首先要在AndroidMainFest.xml添加接收和发送权限
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
        //构建一个SMSManager,用来发送短信
        SmsManager manager = SmsManager.getDefault();
        //自定一两个intent,发送两个隐式意图,这两个隐式意图自己定义
        Intent intent1 =  new Intent("com.example.SENT");;
        Intent intent2 = new Intent("com.example.DELIVERY");
        //延迟意图 sendTextMessage()方法后边两个参数pi1和pi2
        为pendingIntent 延迟意图这两个延迟意图用来查看短信是否
        发出和接收pi1在发送者发送信息送达基站时,会调用,标志着短信
        已经发出pi2是从基站送达接收者手机时会被调用,标志着短信已
        经由接收者接收,因为发送出去的信息不一定会被接收到,所以pi2
        不一定会被调用,这里的pi1和pi2通过广播的方式,通知短信发送的
        相关信息
        PendingIntent pi1 = PendingIntent.getBroadcast(this, 0, intent1 , 0);//延迟意图
        PendingIntent pi2 = PendingIntent.getBroadcast(this, 0, intent2 , 0);
        //发送短信
        manager.sendTextMessage("15555215556", 
                null, 
                etContent.getText().toString(), 
                pi1, 
                pi2);

    }
    //onREsume()方法是Activity运行过程中与用户交互时执行的方
    法,一般可以再次方法中注册broadcastReceiver
    @Override
    protected void onResume() { 
        // TODO Auto-generated method stub
        super.onResume();
        //创建一个自定义的receiver
        receiver = new MyReciver();
        //创建意图过滤器,并把刚才定义的Action添加上,表示只接收
        这些action发送的广播
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.SENT");
        filter.addAction("com.example.DELIVERY");
        //安卓系统后台有一个服务(Service),专门用来接收短消
        息。当有新的消息达到时,它会发送一个广播,广播的
        Action是“android.provider.Telephony.SMS_RECEIV
        ED”并且将收到短消息作为广播Intent的一部分(Intent
        的Extra)发送出去。权限高的先得到
        filter.addAction("android.provider.Telephony.
        SMS_RECEIVED");
        //在这里给filter添加一个最高权限;
        filter.setPriority(Integer.MAX_VALUE);
        //注册receiver
        registerReceiver(receiver, filter);
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        //注销注册
        unregisterReceiver(receiver);
    }
    //既然发送了广播就要有一个广播接收者,自定义一个广播接收者,自定
    义一个类MyReceiver继承BroadcastReceiver
    public class MyReciver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //既然发送了广播就要有一个广播接收者,自定义一个广
            播接收者
            if("com.example.SENT".equals(action)){
                Log.d("TAG", "短信发送成功"+System.currentTimeMillis());
            }
            //如果intent的action是com.example.SENT,表示pi1
            被调用,即短信已经送达基站
            else if("com.example.DELIVERY".equals(action)){
                Log.i("TAG", "对方已经收到短信"+System.currentTimeMillis());
            }

    //如果intent的action是android.provider.Telephony.SMS_R
    ECEIVED,这时我们就可以开始处理短信内容了
    if("android.provider.Telephony.SMS_RECEIVED".equals(action)){
                //处理消息,StringBuilder 对象用来存放短信信息
                //String number 用来存放电话号码
                Bundle bundle = intent.getExtras();
                Object[] pdus = (Object[]) bundle.get("pdus");
                StringBuilder sb = new StringBuilder();
                String number = "";
                //把一个一个的pdus转换为一段段的短消息
                for(int i=0;i<pdus.length;i++){
                    SmsMessage message  = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    //然后把正文拼接起来
                        sb.append(message.getDisplay
MessageBody());             
                    //获取电话号码
                    number = message.getDisplayOriginatingAddress();
                }
                Log.d("TAG", "发送方电话"+number+"内容为"+sb);
            }
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值