packagecom.example.call_msm;importandroidx.annotation.RequiresApi;importandroidx.appcompat.app.AppCompatActivity;importandroidx.core.app.ActivityCompat;importandroid.Manifest;importandroid.annotation.SuppressLint;importandroid.content.Intent;importandroid.content.pm.PackageManager;importandroid.net.Uri;importandroid.os.Build;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Toast;import static android.Manifest.*;public class MainActivity extendsAppCompatActivityimplementsView.OnLongClickListener {privateButton btn_call;privateButton btn_sms;privateEditText editText_number;privateEditText editText_message;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_call=findViewById(R.id.btn_call);
btn_sms=findViewById(R.id.btn_sms);
btn_call.setOnLongClickListener(this);
btn_sms.setOnLongClickListener(this);
editText_number=findViewById(R.id.editText_number);
editText_message=findViewById(R.id.editText_message);
}
@RequiresApi(api=Build.VERSION_CODES.M)
@Overridepublic booleanonLongClick(View v) {if(v.equals(btn_call)) {
makeCall();
}else if(v.equals(btn_sms)) {
sendMessage();
}return true;
}
@RequiresApi(api=Build.VERSION_CODES.M)private voidsendMessage() {if (checkSelfPermission(permission.SEND_SMS) !=PackageManager.PERMISSION_GRANTED) {//TODO: Consider calling//Activity#requestPermissions//here to request the missing permissions, and then overriding//public void onRequestPermissionsResult(int requestCode, String[] permissions,//int[] grantResults)//to handle the case where the user grants the permission. See the documentation//for Activity#requestPermissions for more details.
ActivityCompat.requestPermissions(this, new String[]{permission.SEND_SMS}, 1);//ActivityCompat.shouldShowRequestPermissionRationale(this, permission.CALL_PHONE);
return;
}
Toast.makeText(this, "长按发短信", Toast.LENGTH_SHORT).show();
String phoneNumber=editText_number.getText().toString();
Uri uri= Uri.parse("smsto:" +phoneNumber);
Intent intent= newIntent(Intent.ACTION_SEND, uri);
String msg=editText_message.getText().toString();
intent.putExtra("sms_body", msg);
startActivity(intent);
}
@RequiresApi(api=Build.VERSION_CODES.M)private voidmakeCall() {
Toast.makeText(this, "长按打电话", Toast.LENGTH_SHORT).show();
String phoneNumber=editText_number.getText().toString();
Uri uri= Uri.parse("tel:" +phoneNumber);
Intent intent= newIntent(Intent.ACTION_CALL, uri);if (checkSelfPermission(permission.CALL_PHONE) !=PackageManager.PERMISSION_GRANTED) {//TODO: Consider calling//Activity#requestPermissions//here to request the missing permissions, and then overriding//public void onRequestPermissionsResult(int requestCode, String[] permissions,//int[] grantResults)//to handle the case where the user grants the permission. See the documentation//for Activity#requestPermissions for more details.
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},1);//ActivityCompat.shouldShowRequestPermissionRationale(this, permission.CALL_PHONE);
return;
}
startActivity(intent);
}public voidjumpCall(View view) {
Toast.makeText(this,"打电话",Toast.LENGTH_SHORT).show();
String phoneNumber=editText_number.getText().toString();
Uri uri= Uri.parse("tel:"+phoneNumber);
Intent intent= newIntent(Intent.ACTION_DIAL, uri);
startActivity(intent);
}public voidjumpSendMessage(View view) {
Toast.makeText(this,"发短信",Toast.LENGTH_SHORT).show();
String phoneNumber=editText_number.getText().toString();
Uri uri= Uri.parse("smsto:" +phoneNumber);
Intent intent= newIntent(Intent.ACTION_SENDTO,uri);
String msg=editText_message.getText().toString();
intent.putExtra("sms_body",msg);
startActivity(intent);
}
}