使用案例:发送短信
send_sms.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginLeft="32dp" android:layout_marginTop="76dp" android:text="电话号码:" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/phoneNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginLeft="24dp" android:layout_marginTop="52dp" android:layout_marginEnd="76dp" android:layout_marginRight="76dp" android:ems="10" android:hint="请输入" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toEndOf="@+id/textView2" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="60dp" android:layout_marginLeft="60dp" android:layout_marginTop="52dp" android:text="信息:" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <EditText android:id="@+id/message" android:layout_width="212dp" android:layout_height="129dp" android:layout_marginStart="24dp" android:layout_marginLeft="24dp" android:layout_marginTop="36dp" android:ems="10" android:gravity="start|top" android:hint="请输入" android:inputType="textMultiLine" app:layout_constraintStart_toEndOf="@+id/textView3" app:layout_constraintTop_toBottomOf="@+id/phoneNumber" /> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="248dp" android:layout_marginLeft="248dp" android:layout_marginTop="68dp" android:onClick="handleSend" android:text="发送" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/message" /> </androidx.constraintlayout.widget.ConstraintLayout> import android.Manifest; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.Arrays; public class PermisssionActivity extends BaseActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.send_sms); } public void sendSms(String phone, String msg) { SmsManager smsManager = SmsManager.getDefault(); ArrayList<String> stringList = smsManager.divideMessage(msg); for (String cotentFragment : stringList) { smsManager.sendTextMessage(phone, null, cotentFragment, null, null); } Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show(); } public void handleSend(View view) { final EditText phoneNumber = findViewById(R.id.phoneNumber); final EditText message = findViewById(R.id.message); requestPermission(new PermissionCallback() { @Override public void hasPermission() { sendSms(phoneNumber.getText().toString().trim(), message.getText().toString().trim()); } @Override public void noPermission(String... deniedPermissions) { Toast.makeText(getContext(), "没有授权:" + Arrays.toString(deniedPermissions), Toast.LENGTH_LONG).show(); } }, Manifest.permission.SEND_SMS); } }