码迷,mamicode.com
首页 > 其他好文 > 详细

发短信和打电话

时间:2020-04-13 19:51:11      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:mamicode   new   odi   coding   override   super   version   you   match   

我们做一个这样的界面

技术图片

 

 

1、点击按钮,可以唤起 打电话 、发短信的界面

2、长按按钮,直接拨号和发送短信

我们通过在界面上绑定点击监听

            <Button
                android:id="@+id/btn_call"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拨打电话"
                android:onClick="jumpCall"/>
            <Button
                android:id="@+id/btn_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="发送短信"
                android:onClick="jumpSendMessage"/>

 

通过实现 OnLongClickListener接口,绑定长按监听

技术图片

 

 然后进行绑定

btn_call.setOnLongClickListener(this);
btn_sms.setOnLongClickListener(this);

 

然后实现方法即可。这里要注意的是,Android 6.0 之后,在申明权限外,还要动态申请权限

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;
}

 

 给出完整的界面布局

技术图片
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="请输入电话号码:">

            </TextView>
            <EditText
                android:id="@+id/editText_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="请输入电话号码"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="请输入短信内容:"/>
            <EditText
                android:id="@+id/editText_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="在此输入短信内容"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn_call"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拨打电话"
                android:onClick="jumpCall"/>
            <Button
                android:id="@+id/btn_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="发送短信"
                android:onClick="jumpSendMessage"/>
        </LinearLayout>


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
布局

 

完整的mainActivity.java

技术图片
package com.example.call_msm;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import static android.Manifest.*;

public class MainActivity extends AppCompatActivity
        implements View.OnLongClickListener {

    private Button btn_call;
    private Button btn_sms;
    private EditText editText_number;
    private EditText editText_message;

    @Override
    protected void onCreate(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)
    @Override
    public boolean onLongClick(View v) {
        if (v.equals(btn_call)) {
            makeCall();
        } else if (v.equals(btn_sms)) {
            sendMessage();
        }

        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void sendMessage() {
        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 = new Intent(Intent.ACTION_SEND, uri);
        String msg = editText_message.getText().toString();
        intent.putExtra("sms_body", msg);
        startActivity(intent);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void makeCall() {
        Toast.makeText(this, "长按打电话", Toast.LENGTH_SHORT).show();
        String phoneNumber = editText_number.getText().toString();
        Uri uri = Uri.parse("tel:" + phoneNumber);
        Intent intent = new Intent(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 void jumpCall(View view) {
        Toast.makeText(this,"打电话",Toast.LENGTH_SHORT).show();
        String phoneNumber = editText_number.getText().toString();
        Uri uri = Uri.parse("tel:"+phoneNumber);
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        startActivity(intent);

    }

    public void jumpSendMessage(View view) {
        Toast.makeText(this,"发短信",Toast.LENGTH_SHORT).show();
        String phoneNumber = editText_number.getText().toString();
        Uri uri = Uri.parse("smsto:" + phoneNumber);
        Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
        String msg = editText_message.getText().toString();
        intent.putExtra("sms_body",msg);
        startActivity(intent);
    }
}
mainActivity.java

 

发短信和打电话

标签:mamicode   new   odi   coding   override   super   version   you   match   

原文地址:https://www.cnblogs.com/superxuezhazha/p/12693284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!