Android中发送短信和拨打电话

我将打电话和发送短信封装成了一个类,使用时直接可以调用。需要注意的是,一定要记得在清单文件中添加权限,Android6.0之后的权限需要动态添加,不然无法发送短息和拨打电话。权限设置可以自行百度。

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.telephony.SmsManager;

import java.util.ArrayList;

/**
 * @author 汪书北
 * @time 2019年12月16日
 * <p>
 * 使用前需要在manifast文件中添加权限。
 * <uses-permission android:name="android.permission.SEND_SMS"/>
 * <uses-permission android:name="android.permission.CALL_PHONE"/>
 * </p>
 */
public class SmsAndCallTools {

    /**
     * <p>这是一个直接发送短信的方法,不需要打开系统短信界面</p>
     *
     * @param phoneNumber 接收方的电话号码
     * @param content     短信内容
     */

    public static boolean Send_SMS_directly(String phoneNumber, String content) {
        try {
            //1、添加发送短信的权限在manifast文件中。
            // <uses-permission android:name="android.permission.SEND_SMS"/>
            // 2、获取android.telephony.SmsManager对象(PS:android.telephony.gsm.SmsManager已经废弃)。
            SmsManager smsManager = SmsManager.getDefault();
            // 3、声明一个短信内容的常量。
            // String content = "Hello World!";
            // 4、将短信内容分块,发送一条短信最多能够发送70个中文字符,超过这个值系统会将短信内容分为多块进行发送。
            ArrayList<String> list = smsManager.divideMessage(content);
            // 5、分条进行发送。
            for (int i = 0; i < list.size(); i++) {
                smsManager.sendTextMessage(phoneNumber, null, list.get(i), null, null);
            }

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 指定接收者的号码,然后发送短信。需要打开系统短信界面
     *
     * @param activity
     * @param phoneNumber 接收者的号码
     * @return
     */

    public static boolean Send_SMS_phoneNumber(Activity activity, String phoneNumber) {
        try {
            //1、创建Uri,设置行为和号码
            Uri uri = Uri.parse("smsto:" + phoneNumber);
            // 2、创建意图。
            Intent intentMessage = new Intent(Intent.ACTION_VIEW, uri);
            // 3、打开系统短信界面,号码已经填写,只需填写要发送
            activity.startActivity(intentMessage);

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 指定发送内容,然后发送短信。需要打开系统短信界面
     *
     * @param activity
     * @param body     发送的内容
     * @return
     */
    public static boolean Send_SMS_Body(Activity activity, String body) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setType("text/plain");
            intent.putExtra("sms_body", body);
            activity.startActivity(intent);

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 指定发送内容和号码,然后发送短信。需要打开系统短信界面
     *
     * @param activity
     * @param phoneNumber 号码
     * @param body        内容
     * @return
     */
    public static boolean Send_SMS_phoneNumber_Body(Activity activity, String phoneNumber, String body) {
        try {

            //使用Intent调用发送短信的功能
            Intent intent = new Intent();
            //设置Action和Uri
            intent.setAction(Intent.ACTION_SENDTO);//设置数据
            intent.setData(Uri.parse("smsto:" + phoneNumber));
            //指定短信的内容
            intent.putExtra("sms_body", body);
            activity.startActivity(intent);

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 这是一个拨打电话的方法
     *
     * @param activity
     * @param phoneNumber 电话号码
     * @return
     */


    public static boolean call(Activity activity, String phoneNumber) {
        try {

            //使用Intent调用打电话的功能
            //在manifast文件中添加权限
            // <uses-permission android:name="android.permission.CALL_PHONE"/>
            Intent intent = new Intent();
            //设置Action和Uri
            intent.setAction(Intent.ACTION_CALL);
            //设置数据
            intent.setData(Uri.parse("tel:" + phoneNumber));
            activity.startActivity(intent);

            return true;
        } catch (Exception e) {
            return false;
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值