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