iOS
- (void)telphone
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"請(qǐng)選擇要撥打的號(hào)碼" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// phones : 電話對(duì)象的數(shù)組
// phone : 電話對(duì)象
for (Phone *phone in phones) {
UIAlertAction *judgeCode = [UIAlertAction actionWithTitle:phone.phoneNumber style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//撥打電話璃岳、本質(zhì)就是調(diào)用內(nèi)置的打電話應(yīng)用
NSString *str = [NSString stringWithFormat:@"tel:%@",phone.phoneNumber]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}];
[alertController addAction:judgeCode];
}
}
- (void)telphone
{
//若直接撥打某一個(gè)電話可直接調(diào)用
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:177****5923"]];
}
- (void)sendSMS{
//發(fā)短信
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",@"177****2592"]];
[[UIApplication sharedApplication] openURL:url];
}
Android
//打電話
private void telPhone() {
//在AndroidManifest.xml中添加權(quán)限
// <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); //這就是內(nèi)部類訪問(wèn)外部類的實(shí)例
}
//發(fā)短信
private void sendSms(){
//在AndroidManifest.xml中添加權(quán)限
// <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
//處理返回的發(fā)送狀態(tài)
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,
"短信發(fā)送成功", 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));
//處理返回的接收狀態(tài)
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,
"收信人已經(jīng)成功接收", Toast.LENGTH_SHORT)
.show();
}
}, new IntentFilter(DELIVERED_SMS_ACTION));
SmsManager sm = SmsManager.getDefault();
/*
* arg0:目標(biāo)號(hào)碼
* arg1:短信中心號(hào)碼偏友,null表示使用默認(rèn)
* arg2:短信正文
* arg3:可為空扛或,不為空是為返回的發(fā)送狀態(tài)廣播
* arg4:可為空,不為空是為返回的接受狀態(tài)廣播
* */
//把長(zhǎng)短信截成若干條短短信,短信太長(zhǎng)瞒大,運(yùn)營(yíng)商是不會(huì)發(fā)送的,需要我們自己截取
ArrayList<String> sms = sm.divideMessage("短信內(nèi)容");
for (String string : sms){
sm.sendTextMessage("17762405923",null,string,sentPI, deliverPI);
}
}