【iOS && Android】電話撥號(hào)tel與短信發(fā)送sms

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)部類訪問外部類的實(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);
        }
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,872評(píng)論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,842評(píng)論 2 45
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,144評(píng)論 22 665
  • 今天有幸找到了「失控.全人類的最終命運(yùn)和結(jié)局」一書。 書中寫到去中心化,以最少的規(guī)則來完成任務(wù),可以有效的避免環(huán)境...
    Helexy22閱讀 497評(píng)論 0 0
  • 新加坡的語言環(huán)境非常多元,華族同胞們說的華語就與中國(guó)大陸的普通話有很大的不同。在這里引用周清海老師編著的《新加坡華...
    祺祺qi閱讀 1,305評(píng)論 0 2

友情鏈接更多精彩內(nèi)容