iOS
- (void)telphone
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择要拨打的号码" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// phones : 电话对象的数组
// phone : 电话对象
for (Phone *phone in phones) {
UIAlertAction *judgeCode = [UIAlertAction actionWithTitle:phone.phoneNumber style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//拨打电话、本质就是调用内置的打电话应用
NSString *str = [NSString stringWithFormat:@"tel:%@",phone.phoneNumber]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}];
[alertController addAction:judgeCode];
}
}
- (void)telphone
{
//若直接拨打某一个电话可直接调用
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:177****5923"]];
}
- (void)sendSMS{
//发短信
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",@"177****2592"]];
[[UIApplication sharedApplication] openURL:url];
}
Android
//打电话
private void telPhone() {
//在AndroidManifest.xml中添加权限
// <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "177****5923"));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(intent); //这就是内部类访问外部类的实例
}
//发短信
private void sendSms(){
//在AndroidManifest.xml中添加权限
// <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
//处理返回的发送状态
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,
0);
// register the Broadcast Receivers
this.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context,
"短信发送成功", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
//处理返回的接收状态
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
// create the deilverIntent parameter
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0,
deliverIntent, 0);
this.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,
"收信人已经成功接收", Toast.LENGTH_SHORT)
.show();
}
}, new IntentFilter(DELIVERED_SMS_ACTION));
SmsManager sm = SmsManager.getDefault();
/*
* arg0:目标号码
* arg1:短信中心号码,null表示使用默认
* arg2:短信正文
* arg3:可为空,不为空是为返回的发送状态广播
* arg4:可为空,不为空是为返回的接受状态广播
* */
//把长短信截成若干条短短信,短信太长,运营商是不会发送的,需要我们自己截取
ArrayList<String> sms = sm.divideMessage("短信内容");
for (String string : sms){
sm.sendTextMessage("17762405923",null,string,sentPI, deliverPI);
}
}