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