在AndroidManifest.xml文件向系統註冊一常駐的receiver,並設置這個receiver的intent-filter名稱爲android.provider.Telephony.SMS_RECEIVED,
這樣就可以讓該receiver對收到短信事件進行處理。
主activity
public class EX06_01 extends Activity
{
private TextView mTextView1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*透過findViewById建構巳建立TextView對象*/
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mTextView1.setText("等待接收短信...");
}
}
一個繼承BroadcastReceiver的類
package irdc.EX06_01;
/*必需引用BroadcastReceiver類*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
/*必需引用telephoney.gsm.SmsMessage來收取短信*/
import android.telephony.SmsMessage;
/*必需引用Toast類來監聽用戶收到短信*/
import android.widget.Toast;
/* 自定義繼承自BroadcastReceiver類,監聽系統服務廣播的信息 */
public class EX06_01_SMSreceiver extends BroadcastReceiver
{
/*聲明靜態字符串,並使用android.provider.Telephony.SMS_RECEIVED作爲Action爲短信的依據*/
private static final String mACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
/* 判斷傳來Intent是否爲短信*/
if (intent.getAction().equals(mACTION))
{
/*建構一字符串集集合變量sb*/
StringBuilder sb = new StringBuilder();
/*接收由Intent傳來的數據*/
Bundle bundle = intent.getExtras();
/*判斷Intent是有資料*/
if (bundle != null)
{
/* pdus爲 android內建短信參數 identifier
* 透過bundle.get("")並傳一個包含pdus的對象*/
Object[] myOBJpdus = (Object[]) bundle.get("pdus");
/*建構短信對象array,並依據收到的對象長度來建立array的大小*/
SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
for (int i = 0; i<myOBJpdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu ((byte[]) myOBJpdus[i]);
}
/* 將送來的短信合併自定義信息於StringBuilder當中 */
for (SmsMessage currentMessage : messages)
{
sb.append("接收到來告:\n");
/* 來訊者的電話號碼 */
sb.append(currentMessage.getDisplayOriginatingAddress());
sb.append("\n------傳來的短信------\n");
/* 取得傳來訊息的BODY */
sb.append(currentMessage.getDisplayMessageBody());
}
}
/* 北Notification(Toase)顯示短信信息 */
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
/* 返並加Activity */
Intent i = new Intent(context, EX06_01.class);
/*設定讓加Activity以一個新的task來執行*/
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="irdc.EX06_01"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EX06_01"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 建立receiver聆聽系統廣播訊息 -->
<receiver android:name="EX06_01_SMSreceiver">
<!-- 設定要捕捉的訊息名稱為provider中Telephony.SMS_RECEIVED -->
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="irdc.EX06_01"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EX06_01"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 建立receiver聆聽系統廣播訊息 -->
<receiver android:name="EX06_01_SMSreceiver">
<!-- 設定要捕捉的訊息名稱為provider中Telephony.SMS_RECEIVED -->
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>