android 实现发送短信功能

布局如图:        To:________________             //输入手机号

                             _______________ Send            //输入短信内容,点击Send按钮发送信息

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="To:"
        android:padding="10dp"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/to"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:padding="10dp"
        android:id="@+id/msg_input"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/send"
        android:text="Send"
        android:textAllCaps="false"
        />
</LinearLayout>
在MainActivity中:
send=(Button)findViewById(R.id.send);
to=(EditText)findViewById(R.id.to);
msgInput=(EditText)findViewById(R.id.msg_input);
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();//获取SmsManager的实例
        Intent sentIntent=new Intent("SENT_SMS_ACTION");
        PendingIntent pi=PendingIntent.getBroadcast(MainActivity.this,0,sentIntent,0);
        smsManager.sendTextMessage(to.getText().toString(),null,msgInput.getText().toString(),pi,null);
    }
});
 @Override
    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(sendStatusReceiver);
    }
    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();
                to.setText("");          //短信发送后清除其内容
                msgInput.setText("");
            }else{
                Toast.makeText(context,"Send failed",Toast.LENGTH_LONG).show();
            }
        }
    }
}
声明权限:
<uses-permission android:name="android.permission.SEND_SMS"/>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值