一,需求
点击“打电话”进入拨号界面
长按“打电话”直接拨打电话
点击“发短信”进入编辑短信界面
长按“发短信”直接将短信发送出去
二,代码模块
AndroidManifest
<!--打电话的权限 --><uses-permission android:name="android.permission.CALL_PHONE"/><!--发短信的权限 --><uses-permission android:name="android.permission.SEND_SMS"/>
MainActivity
//初始化需要操作的视图对象private EditText edit_main_number;private EditText edit_main_sms;private Button btn_main_call;private Button btn_main_send;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//得到视图对象edit_main_number = (EditText) findViewById(R.id.edit_main_number);edit_main_sms = (EditText) findViewById(R.id.edit_main_sms);btn_main_call = (Button) findViewById(R.id.btn_main_call);btn_main_send = (Button) findViewById(R.id.btn_main_send);//给视图对象设置点击监听(点击跳转到拨号界面)btn_main_call.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//String action = "android.intent.action.DIAL";String action = Intent.ACTION_DIAL;//与上面效果相同//1.创建一个Intent(隐式)Intent intent = new Intent(action);//2.携带数据(两种方式)//intent.putExtra();String number = edit_main_number.getText().toString();intent.setData(Uri.parse("tel:" + number));//3.startActivity(intent)startActivity(intent);}});//给视图对象设置点击监听(点击跳转到编辑短信界面)btn_main_send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(Intent.ACTION_SENDTO);//携带数据String number = edit_main_number.getText().toString();//号码String sms = edit_main_sms.getText().toString();//内容intent.setData(Uri.parse("smsto:" + number));//携带号码//携带额外数据intent.putExtra("sms_body", sms);startActivity(intent);}});//长按打电话if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) !=PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);} else {//给视图对象设置长按监听(长按打电话)btn_main_call.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View view) {Intent intent = new Intent(Intent.ACTION_CALL);String number = edit_main_number.getText().toString();intent.setData(Uri.parse("tel:" + number));startActivity(intent);return true;}});}//长按发短信if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 1);} else {//给视图对象设置长按监听(长按发短信)btn_main_send.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View view) {//1.得到SmsManager的对象SmsManager smsManager = SmsManager.getDefault();//2.发送文本信息短信String number = edit_main_number.getText().toString();//号码String sms = edit_main_sms.getText().toString();//内容smsManager.sendTextMessage(number, null, sms, null, null);Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT).show();return true;}});}
activity_main.xml
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="电话号码:" /><EditTextandroid:id="@+id/edit_main_number"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="请输入电话号码"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="短信内容:" /><EditTextandroid:id="@+id/edit_main_sms"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="请输入短信内容" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_main_call"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="打电话" /><Buttonandroid:id="@+id/btn_main_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发短信" /></LinearLayout>
三,演示效果
点击“打电话”:
其他效果自行测试!