<uses-permission android:name="android.permission.CALL_PHONE"/><uses-permission android:name="android.permission.SEND_SMS"/>
package com.example.call_sms; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.telephony.SmsManager; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnLongClickListener { private EditText text_main_phone; private EditText text_main_sms; private Button btn_main_call; private Button btn_main_sms; private String[] permissions = {Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS}; public static final int REQUEST_CALL_CODE = 10086; public static final int REQUEST_SMS_CODE = 10010; View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { if (v == btn_main_call) { //跳转到打电话界面 String action = Intent.ACTION_DIAL; String phone = text_main_phone.getText().toString(); Uri uri = Uri.parse("tel:" + phone); Intent intent = new Intent(action, uri); startActivity(intent); } else if (v == btn_main_sms) { //跳转到发短信界面 String action = Intent.ACTION_SENDTO; String phone = text_main_phone.getText().toString(); String sms_body = text_main_sms.getText().toString(); Uri uri = Uri.parse("smsto:" + phone); Intent intent = new Intent(action, uri); intent.putExtra("sms_body", sms_body); startActivity(intent); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text_main_phone = (EditText) findViewById(R.id.text_main_phone); text_main_sms = (EditText) findViewById(R.id.text_main_sms); btn_main_call = (Button) findViewById(R.id.btn_main_call); btn_main_sms = (Button) findViewById(R.id.btn_main_sms); btn_main_call.setOnClickListener(onClickListener); btn_main_sms.setOnClickListener(onClickListener); btn_main_call.setOnLongClickListener(this); btn_main_sms.setOnLongClickListener(this); } @Override public boolean onLongClick(View v) { if (v == btn_main_call) { onCall(); } else if (v == btn_main_sms) { onSendSms(); } return true; } public void onCall() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, permissions, REQUEST_CALL_CODE); } else { String action = Intent.ACTION_CALL; String phone = text_main_phone.getText().toString(); Uri uri = Uri.parse("tel:" + phone); Intent intent = new Intent(action, uri); startActivity(intent); } } public void onSendSms() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, permissions, REQUEST_SMS_CODE); } else { String phone = text_main_phone.getText().toString(); String sms_body = text_main_sms.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phone, null, sms_body, null, null); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case REQUEST_CALL_CODE: if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { onCall(); } else { Log.d("TAG", "Call Permission Not Granted"); } break; case REQUEST_SMS_CODE: if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { onSendSms(); } else { Log.d("TAG", "SMS Permission Not Granted"); } break; default: break; } } }