安卓打電話,發簡訊

神棍先生發表於2017-09-19

版權宣告:本文可能為博主原創文章,若標明出處可隨便轉載。 https://blog.csdn.net/Jailman/article/details/78027636

package online.geekgalaxy.layoutlearn;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jailman on 2017/9/18.
 */

public class login extends Activity {

    public void SendSMS(String PhoneNumber, String SMS) {
        SmsManager sm = SmsManager.getDefault();
        if (isPhoneNumberValid(PhoneNumber) && isWithin70(SMS)) {
            /**
             * 當兩個判定條件都通過時傳送簡訊,先構建一個PendingIntent物件並使用getBroadcast()廣播
             * 然後將PendingIntent,簡訊,電話號碼等內容傳入SmsManager的sendTextMessage()方法中*/
            try {
                PendingIntent pi = PendingIntent.getBroadcast(login.this, 0, new Intent(), 0);
                sm.sendTextMessage(PhoneNumber, null, SMS, pi, null);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Toast.makeText(login.this, "簡訊傳送成功", Toast.LENGTH_LONG).show();
        } else {
            if (!isPhoneNumberValid(PhoneNumber)) {
                if (!isWithin70(SMS)) {
                    Toast.makeText(login.this, "電話號碼格式錯誤!簡訊內容超過70個字!請改正!!!", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(login.this, "電話號碼格式錯誤!請改正!!!", Toast.LENGTH_LONG).show();
                }
            } else {
                if (!isWithin70(SMS)) {
                    Toast.makeText(login.this, "簡訊內容超過70個字!請改正", Toast.LENGTH_LONG).show();
                }
            }
        }
    }


    //判斷簡訊內容是否超過70個字
    public static boolean isWithin70(String s) {
        return s.length() <= 70;
    }

    //判斷電話號碼的格式是否正確
    public static boolean isPhoneNumberValid(String phoneNumber) {
        boolean valid = false;
        /**
         * 兩種電話號碼格式
         * ^\(? 表示可以以(開頭
         * (\d{3}) 表示後面緊跟3個數字
         * \)? 表示可以以)繼續
         * [- ]? 表示在上述格式後面可以使用選擇性的“-”繼續
         * (\d{4}) 表示後面緊跟4個數字
         * [- ]? 表示在上述格式後面可以使用選擇性的“-"繼續
         * (\d{4})$ 表示以4個數字結束
         * 綜上所述:正確的電話號碼的格式可以以下面等幾種做為參考:
         * (123)456-78900 123-456-78900 12345678900 (123)-456-78900
         * */
        String expression01 = "^\(?(\d{3})\)?[- ]?(\d{4})[- ]?(\d{4})$";
        String expression02 = "^\(?(\d{3})\)?[- ]?(\d{5})[- ]?(\d{5})$";
        //建立Pattern物件
        Pattern p01 = Pattern.compile(expression01);
        //將Pattern作為引數傳入Matcher,當做電話號碼phoneNumber的正確格式
        Matcher m01 = p01.matcher(phoneNumber);
        Pattern p02 = Pattern.compile(expression02);
        Matcher m02 = p02.matcher(phoneNumber);
        valid = m01.matches() || m02.matches();
        return valid;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        //sms button
        final Button bomb = (Button) findViewById(R.id.button4);
        bomb.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String PN = "13xxxxxxx";
                String SMS = "我要用簡訊轟炸你!這個是安卓發簡訊功能!";
                SendSMS(PN, SMS);
            }
        });

        //call button
        final Button call = (Button) findViewById(R.id.button5);
        call.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + "13xxxxxx");
                intent.setData(data);
                if (ActivityCompat.checkSelfPermission(login.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    Toast.makeText(login.this, "Call permission denied!", Toast.LENGTH_LONG).show();
                    return;
                }
                startActivity(intent);
        }
    });



}
}


相關文章