android 拨打电话 发送短信 权限,Android中发送短信和拨打电话

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日

*

* 使用前须要在manifast文件中添加权限。

*

*

*

*/

public class SmsAndCallTools {

/**

*

这是一个直接发送短信的方法,不须要打开系统短信界面

*

* @param phoneNumber 接收方的电话号码

* @param content 短信内容

*/

public static boolean Send_SMS_directly(String phoneNumber, String content) {

try {

//一、添加发送短信的权限在manifast文件中。

//

// 二、获取android.telephony.SmsManager对象(PS:android.telephony.gsm.SmsManager已经废弃)。

SmsManager smsManager = SmsManager.getDefault();

// 三、声明一个短信内容的常量。

// String content = "Hello World!";

// 四、将短信内容分块,发送一条短信最多可以发送70个中文字符,超过这个值系统会将短信内容分为多块进行发送。

ArrayList list = smsManager.divideMessage(content);

// 五、分条进行发送。

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 {

//一、建立Uri,设置行为和号码

Uri uri = Uri.parse("smsto:" + phoneNumber);

// 二、建立意图。

Intent intentMessage = new Intent(Intent.ACTION_VIEW, uri);

// 三、打开系统短信界面,号码已经填写,只需填写要发送

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文件中添加权限

//

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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值