android调用系统发送短信
思路
1、获取用户输入的将要接收短信的号码;
2、获取用户输入的将要发生发送的短信内容
3、利用 Intent 调用系统发送短信;
实践
1、layout布局
<EditText
android:id="@+id/tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="接收短信号码"/>
<EditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="短信内容"/>
<Button
android:id="@+id/goto_short_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送短信"/>
2、java代码
String tels = tel.getText().toString(); //获取电话
String contents = content.getText().toString(); //获取短信内容
Intent intent = new Intent(); //创建 Intent 实例
intent.setAction(Intent.ACTION_SENDTO); //设置动作为发送短信
intent.setData(Uri.parse("smsto:"+tels)); //设置发送的号码
intent.putExtra("sms_body", contents); //设置发送的内容
startActivity(intent); //启动 Activity
tel 和 content 都是文本输入框 EditText ,
其中 smsto 和 sms_body 都不能写错了,不然跳转到系统之后就会丢失信息
3、AndroidManifest.xml权限设置
<uses-permission android:name="android.permission.SEND_SMS" />
源码:https://github.com/iscopy/Blog
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_41454168/article/details/79525819