2.SMSReceiver.java
package tw.nicky.ReceiveSMSExample; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.widget.Toast; public class SMSReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //判斷這個是接收到SMS的Broadcast if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) { Bundle bundle = intent.getExtras(); Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] smsMessages = new SmsMessage[pdus.length]; //將pdu轉成SmsMessage for(int i=0;i3. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tw.nicky.ReceiveSMSExample" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="ReceiveSMSExample"> <receiver android:name=".SMSReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.RECEIVE_SMS" /> </manifest>5. 執行畫面
沒有留言:
張貼留言