android基础知识点复习之短信发送
2023-09-14 08:59:01 时间
界面布局:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/phonenumber_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/phonenumber" /> <EditText android:id="@+id/phonenumber_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/phonenumber_text" android:inputType="phone" > </EditText> <TextView android:id="@+id/phonenumber_text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/phonenumber_edit" android:text="@string/phonenumber" /> <EditText android:id="@+id/message_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/phonenumber_text2" android:lines="5" /> <Button android:id="@+id/call_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/message_edit" android:onClick="callphone" android:text="call" /> </RelativeLayout>
java代码:
import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; public class SendMessage extends Activity implements OnClickListener { private EditText phonenumber_edit; private EditText content_eidt; private Button Send_button; private String content ; private String phoneNumber; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } /** * * @Title: initViews * @Description: TODO(初始化布局文件) * @param 设定文件 * @return void 返回类型 * @throws */ private void initViews() { phonenumber_edit = (EditText)findViewById(R.id.phonenumber_edit); content_eidt = (EditText)findViewById(R.id.message_edit); Send_button = (Button)findViewById(R.id.call_button); Send_button.setOnClickListener(this); } /** * * @Name onClick * @Description TODO(按钮单击事件处理) * @param v * @see android.view.View.OnClickListener#onClick(android.view.View) * @Date 2013-12-8 下午10:28:30 * */ @SuppressLint("NewApi") @Override public void onClick(View v) { switch (v.getId()) { //打你发送短信按钮 case R.id.call_button: //取得短信内容 content = content_eidt.getText().toString().trim(); //取得手机号码 phoneNumber = phonenumber_edit.getText().toString().trim(); //判断字符是否为空 if(TextUtils.isEmpty(content)||TextUtils.isEmpty(phoneNumber)){ Toast.makeText(getApplicationContext(), "请输入内容", Toast.LENGTH_LONG).show(); return; } //短信发送 SmsManager smsManager = SmsManager.getDefault(); ArrayList<String> contents = smsManager.divideMessage(content); for (String string : contents) { smsManager.sendTextMessage(phoneNumber, null, string, null, null); } Toast.makeText(getApplicationContext(), "短信已经发送", Toast.LENGTH_LONG).show(); break; default: break; } } }
权限设置:
<!-- 发送短信权限的权限 --> <uses-permission android:name="android.permission.SEND_SMS" />
相关文章
- [Android Pro] android 4.4 Android原生权限管理:AppOps
- 中国移动MM Android/OPhone付费SDK--MMBillingSDK,集成问题总结
- Android sendevent/getevent 用法
- 使用Android Studio搭建Android集成开发环境(图文教程)
- android EventBus详解(二)
- Android 开发之旅:短信的收发及在android模拟器之间实践(一)
- Android 开发之旅:短信的收发及在android模拟器之间实践(二)
- Android应用开发揭秘之优化技术
- 2013最新Android常用的工具类整理
- 《android开发艺术探索》读书笔记(十五)--Android性能优化
- android 编程时,如何在Eclipse中查看Android源码或者第三方组件包源码
- 43、android:screenOrientation
- 《android开发艺术探索》读书笔记(十)--Android的消息机制
- 2022最全Android中高级开发知识点总结(超详细整理)
- Android App调用MediaRecorder实现录音功能的实例
- Android 编译之android.bp详解
- Android 去掉RadioButton水波纹效果
- Android 11.0 12.0修改系统默认设备类型的平板电脑类型为设备类型
- Android 自定义ViewPager(仿蘑菇街欢迎页)
- Android 9.0 10.0 去掉开机提示 android is starting 的对话框
- Android 9.0静默安装与卸载app
- 【Android 内存优化】Bitmap 图像尺寸缩小 ( 设置 Options 参数 | inJustDecodeBounds | inSampleSize | 工具类实现 )
- Android 开发 --Unable to resolve target 'android-19'
- android开发笔记
- android开发,Android Studio Build Output 输出的中文显示乱码