Activity文件
package com.example.administrator.jackapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class messageActivity extends AppCompatActivity {
/**
* onCreate方法在Activity创建时被系统调用
* 可以做一些初始化操作
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置Activity布局文件
setContentView(R.layout.activity_message);
// 声明一个Button对象button_tel,并通过资源id得到Button实例
Button button_mess = (Button)findViewById(R.id.bt_mess);
// 设置button_tel的点击事件监听
button_mess.setOnClickListener(new buttonOnClickListener());
}
/**
* 内部类 buttonOnClickListener
* 监听Button点击事件
* 内部类重写 onClick方法
*/
class buttonOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
EditText editText_tel = (EditText)findViewById(R.id.et_mess_tel);
EditText editText_mess = (EditText)findViewById(R.id.et_mess_context);
String mess_tel = editText_tel.getText().toString();
String mess_text = editText_mess.getText().toString();
//使用发短信API
//通过getDefault()方法创建SmsManager对象
SmsManager smsManager = SmsManager.getDefault();
//拆分长短信
ArrayList<String> messes = smsManager.divideMessage(mess_text);
for (String str:messes) {
//发送短信
smsManager.sendTextMessage(mess_tel,null,str,null,null);
}
// 清空editText_mess中的内容
editText_mess.setText("");
}
}
}
package com.example.administrator.jackapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class messageActivity extends AppCompatActivity {
/**
* onCreate方法在Activity创建时被系统调用
* 可以做一些初始化操作
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置Activity布局文件
setContentView(R.layout.activity_message);
// 声明一个Button对象button_tel,并通过资源id得到Button实例
Button button_mess = (Button)findViewById(R.id.bt_mess);
// 设置button_tel的点击事件监听
button_mess.setOnClickListener(new buttonOnClickListener());
}
/**
* 内部类 buttonOnClickListener
* 监听Button点击事件
* 内部类重写 onClick方法
*/
class buttonOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
EditText editText_tel = (EditText)findViewById(R.id.et_mess_tel);
EditText editText_mess = (EditText)findViewById(R.id.et_mess_context);
String mess_tel = editText_tel.getText().toString();
String mess_text = editText_mess.getText().toString();
//使用发短信API
//通过getDefault()方法创建SmsManager对象
SmsManager smsManager = SmsManager.getDefault();
//拆分长短信
ArrayList<String> messes = smsManager.divideMessage(mess_text);
for (String str:messes) {
//发送短信
smsManager.sendTextMessage(mess_tel,null,str,null,null);
}
// 清空editText_mess中的内容
editText_mess.setText("");
}
}
}
layout文件
<?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:orientation="vertical"
tools:context=".messageActivity">
<EditText
android:id="@+id/et_mess_tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="收件人"
android:inputType="phone" />
<EditText
android:id="@+id/et_mess_context"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="短信内容"
android:lines="5" />
<Button
android:id="@+id/bt_mess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="发送短信" />
</LinearLayout>
manifest文件中增家发送短信权限
<uses-permission android:name="android.permission.SEND_SMS" />