第一步:设置string.xml
<resources> <string name="app_name">03MSG</string> <string name="action_settings">Settings</string> <string name="app_phone">请输入电话号码:</string> <string name="app_content">请输入短信内容</string> <string name="app_send">发送</string> </resources>
第二步:在content_main.xml设置样式。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.jikexueyuan.a03msg.MainActivity" tools:showIn="@layout/activity_main"> <EditText android:id="@+id/bt_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/app_phone" android:inputType="phone"/> <EditText android:id="@+id/bt_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/app_content" android:lines="5"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_send" android:onClick="send"/> </LinearLayout>
第三步:设置MainActivity
package com.example.jikexueyuan.a03msg; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //设置点击属性 public void send(View v){ //获取短信号码和内容 EditText et_phone = (EditText) findViewById(R.id.bt_phone); EditText et_content = (EditText) findViewById(R.id.bt_content); //转换String类型 String phone = et_phone.getText().toString(); String content = et_content.getText().toString(); //设置调用发送接口 SmsManager sm = SmsManager.getDefault(); //设置地址 ArrayList<String> sms =sm.divideMessage(content);
if(content.length<70){
sm.sendTextMessage(phone,null,content,null,null);
}else{ for (String string:sms) { sm.sendTextMessage(phone, null, string, null, null); } } }
第四步:设置AndroidManifes.xml获取SEND_SMS权限。
<uses-permission android:name="android.permission.SEND_SMS"/>