赵雅智:Android短信发送器
16lz
2021-01-26
注意要点:
1)必须要在AndroidManifest.xml中添加发送短信权限<uses-permissionandroid:name="android.permission.SEND_SMS"/>
设置视图:setContentView(R.layout.布局xml文件);
2)查找控件:findViewById(R.id.控件id);
3)监听按钮事件:控件.setOnClickListener(this),实现OnClickListener接口
4)获取editText里的值:getTex();
5)发送短信思路:
a)获取短信号码及内容
b)判断短信号码及内容是否为空
c)获取短息的管理器对象
d)如果你的字符数大于了70拆分
e)发送短信的处理
运行效果如下:
String文件代码:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">lesson02</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="sms_tip_phone">请输入您要发送的手机号码</string> <string name="sms_tip_content">请输入您要发送的信息内容</string> <string name="tip_sms">输入短息的信息</string> <string name="send_sms">发送短信</string> <string name="error_sms_content">您发送的短信号码或者短信内容不能为空</string></resources>
布局文件activity_sms.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="${packageName}.${activityClass}"> <!-- 提示输入电话号码信息 --> <TextView android:id="@+id/tv_tip_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/sms_tip_phone"/> <!-- 输入手机号码的信息 --> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_tip_phone" android:layout_alignParentLeft="true" android:inputType="phone"/> <!-- 提示输入短信信息 --> <TextView android:id="@+id/tv_tip_sms_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/sms_tip_content" android:layout_below="@+id/et_phone"/> <!-- 输入短息的信息 --> <EditText android:id="@+id/et_sms_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_tip_sms_content" android:lines="5" android:hint="@string/tip_sms"/> <!-- 发送信息的按钮 --> <Button android:id="@+id/send_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_sms_content" android:text="@string/send_sms"/></RelativeLayout>
Activity代码:SmsActivity.java
package com.example.lesson02_sms;import java.util.ArrayList;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;public class SmsActivity extends Activity implements OnClickListener{ //声明控件private EditText et_phone;private EditText et_content;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);//设置显示的视图setContentView(R.layout.activity_sms);//获取控件对象et_phone = (EditText) findViewById(R.id.et_phone);et_content = (EditText) findViewById(R.id.et_sms_content);//获取按钮的控件对象Button send_btn = (Button) findViewById(R.id.send_btn);//注册监听事件send_btn.setOnClickListener(this);}/** * 按钮点击事件处理的方法 */@Overridepublic void onClick(View v) {//获取控件的id值 int id = v.getId(); //根据id判断点击的控件 switch (id) {case R.id.send_btn://获取短信号码 String phone = et_phone.getText().toString(); //获取短信的内容 String content = et_content.getText().toString(); //判断短信号码和内容是否为空 if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(content)){ //吐司效果 Toast.makeText(this,R.string.error_sms_content, Toast.LENGTH_SHORT).show(); }else{ //获取短息的管理器对象 SmsManager smsManager = SmsManager.getDefault(); //如果你的字符数大于了70就开始拆了. ArrayList<String> smscontents = smsManager.divideMessage(content); //遍历发送信息 for(int i=0;i<smscontents.size();i++){ //发送短信的处理 smsManager.sendTextMessage(phone, null, smscontents.get(i), null, null); } //吐司效果 Toast.makeText(this, "发送信息成功", Toast.LENGTH_LONG).show(); }break;default:break;}}}
配置文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lesson02_sms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <!-- 发送短信的权限 --> <uses-permission android:name="android.permission.SEND_SMS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.lesson02_sms.SmsActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
更多相关文章
- No resource found that matches the given name (at 'xxx' with
- android 自定义控件的style
- android view获取在屏幕上的绝对坐标
- 【Android开发】网络编程及Internet应用-获取天气预报
- Android本地相册图片URI转换绝对路径
- Android(安卓)获取未读短信(sms)数量
- Android(安卓)根据屏幕尺寸适配控件尺寸(按比例缩放)
- 用python开发android应用 【转载】
- Android(安卓)获取drawable目录图片 并存入指定文件的步骤详解