Skip to content

Instantly share code, notes, and snippets.

@ssd863419
Created October 4, 2014 00:43
Show Gist options
  • Save ssd863419/c5b1394c30682932322a to your computer and use it in GitHub Desktop.
Save ssd863419/c5b1394c30682932322a to your computer and use it in GitHub Desktop.
Android 自制發送短信, SmsManager, PendingIntent
package com.example.administrator.test;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Administrator on 2014/10/3.
* 需要添加發送短信的權限, 寫在AndroidManifest.xml
* <uses-permission android:name="android.permission.SEND_SMS">
*/
public class EX0503 extends Activity {
private EditText mET_receiver;
private EditText mET_major;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex0503);
mET_receiver = (EditText) findViewById(R.id.myET_receiver);
mET_major = (EditText) findViewById(R.id.myET_major);
mButton = (Button) findViewById(R.id.myButton);
mET_major.setText("請輸入短信內容");
mET_receiver.setText("請輸入電話號碼");
mET_major.setOnClickListener(clickEditText);
mET_receiver.setOnClickListener(clickEditText);
mButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
String strPhone = mET_receiver.getText().toString(); //取得收件人號碼
String strMajor = mET_major.getText().toString(); //取得短信內容
SmsManager smsManager = SmsManager.getDefault(); //SmsManager
if (isWithin70(strMajor) && isPhoneNumber(strPhone)) {
try {
/* 創建一個PendingIntent對象, 並使用getBroadcast()廣播
* 使用sendTextMessage()方法發送短信 */
PendingIntent mPI = PendingIntent.getBroadcast(
EX0503.this, 0, new Intent(), 0);
smsManager.sendTextMessage(strPhone, null, strMajor, mPI, null);
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(
EX0503.this,
"發送成功",
Toast.LENGTH_SHORT
).show();
} else {
if (! isWithin70(strMajor)) {
Toast.makeText(
EX0503.this,
"短信內容不可超過70字",
Toast.LENGTH_SHORT
).show();
} else if (! isPhoneNumber(strMajor)) {
Toast.makeText(
EX0503.this,
"電話格式錯誤",
Toast.LENGTH_SHORT
).show();
}
}
}
});
}
EditText.OnClickListener clickEditText = new EditText.OnClickListener() {
@Override
public void onClick(View view) {
if (view.getId() == mET_major.getId()) {
mET_major.setText("");
} else if (view.getId() == mET_receiver.getId()) {
mET_receiver.setText("");
}
}
};
public static boolean isPhoneNumber(String phoneNumber) {
boolean isValue = false;
/* 電話號碼正確格式的正則表達 */
String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
String expression2 = "^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
CharSequence inputStr = phoneNumber;
/* 用Pattern compile第一個正則 */
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
/* 用Pattern compile 第2個正則 */
Pattern pattern2 = Pattern.compile(expression2);
Matcher matcher2 = pattern2.matcher(inputStr);
if (matcher.matches() || matcher2.matches()) {
isValue = true;
}
return isValue;
}
private static boolean isWithin70(String text) {
if (text.length() <= 70) {
return true;
} else {
return false;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收件人"
android:id="@+id/textView"
android:textSize="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myET_receiver"
android:singleLine="false"
android:width="200dp" />
</LinearLayout>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:id="@+id/myET_major"
android:lines="10" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點我傳送信息"
android:id="@+id/mybutton" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment