首先设置以下权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
然后创建以下活动:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="41dp"
android:layout_marginLeft="41dp"
android:layout_marginRight="41dp"
android:layout_marginStart="41dp"
android:layout_marginTop="43dp"
android:ems="10"
android:hint="Enter Phone no"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="37dp"
android:layout_marginLeft="37dp"
android:layout_marginRight="37dp"
android:layout_marginStart="37dp"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Enter text"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="183dp"
android:text="SEND"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
现在转到您的MainActivity.java文件并添加以下代码:
public class MainActivity extends AppCompatActivity {
EditText e1, e2;
Button b1;
private final static int SEND_SMS_PERMISSION_REQ=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.editText);
e2=findViewById(R.id.editText2);
b1=findViewById(R.id.button);
b1.setEnabled(false);
if(checkPermission(Manifest.permission.SEND_SMS))
{
b1.setEnabled(true);
}
else
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQ);
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s1=e1.getText().toString();
String s2=e2.getText().toString();
if(!TextUtils.isEmpty(s1)&&!TextUtils.isEmpty(s2))
{
if(checkPermission(Manifest.permission.SEND_SMS))
{
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(s1,null,s2,null,null);
}
else {
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean checkPermission(String sendSms) {
int checkpermission= ContextCompat.checkSelfPermission(this,sendSms);
return checkpermission== PackageManager.PERMISSION_GRANTED;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case SEND_SMS_PERMISSION_REQ:
if(grantResults.length>0 &&(grantResults[0]==PackageManager.PERMISSION_GRANTED))
{
b1.setEnabled(true);
}
break;
}
}
}
成功地做到了。