1.布局文件代码
<?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"
android:padding="20dp"
tools:context=".MainActivity"
>
<EditText
android:id="@+id/edt_phone"
android:layout_width="match_parent"
android:layout_height="65dp"
android:ems="10"
android:hint="@string/input_phone"
android:inputType="phone" />
<EditText
android:id="@+id/edt_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:lines="6"
android:hint="@string/input_message"
android:inputType="textMultiLine"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_send"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:text="@string/send" />
<Button
android:id="@+id/btn_clear"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:text="@string/clear" />
<Button
android:id="@+id/btn_back"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:text="@string/back" />
</LinearLayout>
</LinearLayout>
2.string.xml代码
call phone
输入你的电话号码
输入短信
发送
清除
返回
设置
package com.example.callphone;
import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.core.app.ActivityCompat;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener {
EditText edtPhone;
EditText edtMessage;
Button btnSend;
Button btnClear;
Button btnBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局文件设置用户界面
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS} , 1);
//获取控件实列
edtPhone=findViewById(R.id.edt_phone);
edtMessage=findViewById(R.id.edt_message);
btnSend=findViewById(R.id.btn_send);
btnClear=findViewById(R.id.btn_clear);
btnBack=findViewById(R.id.btn_back);
//注册监听器
btnSend.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnBack.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_send:
//获取用户输入的电话号码
String strPhone=edtPhone.getText().toString().trim();
//获取用户输入短信
String strMessage=edtMessage.getText().toString().trim();
//获取缺省的短信管理对象
SmsManager smsManager=SmsManager.getDefault();
//创建发送短信的意图
PendingIntent sentIntent=PendingIntent.getBroadcast(MainActivity.this,0,new Intent(),0);
//更具短信的长度决定是否要分条发送
if(strMessage.length()>70){
List<String> messageList=smsManager.divideMessage(strMessage);
for (String message:messageList){
smsManager.sendTextMessage(strPhone,null,message,sentIntent,null);
}
}else{
//发送短信
smsManager.sendTextMessage(strPhone,null,strMessage,sentIntent,null);
}
break;
case R.id.btn_clear:
//清除电话号码编辑框
edtPhone.setText("");
//清除短信编辑框
edtMessage.setText("");
//让电话号码编辑框获得焦点
edtPhone.requestFocus();
break;
case R.id.btn_back:
finish();//关闭窗口
break;
}
}
}
4.要做注册里面写入
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.callphone">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5.代码图片