1. 程式人生 > >android監聽並獲取簡訊

android監聽並獲取簡訊

清單檔案     <!--收簡訊的許可權-->
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <!--讀取簡訊資訊的許可權-->
    <uses-permission android:name="android.permission.READ_SMS"/>  
   <receiver android:name=".business.common.SmsReceiver">
            <intent-filter android:priority="1000"> <!--優先順序:-1000~1000,系統簡訊優先順序為-1-->
                <!--訂閱廣播事件型別-->
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
廣播接收者

public class SmsReceiver extends BroadcastReceiver{
    private static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        //判斷廣播訊息
        if (action.equals(SMS_RECEIVED_ACTION)){
            Bundle bundle = intent.getExtras();
            //如果不為空
            if (bundle!=null){
                //將pdus裡面的內容轉化成Object[]陣列
                Object pdusData[] = (Object[]) bundle.get("pdus");// pdus :protocol data unit  :
                //解析簡訊
                SmsMessage[] msg = new SmsMessage[pdusData.length];
                for (int i = 0;i < msg.length;i++){
                    byte pdus[] = (byte[]) pdusData[i];
                    msg[i] = SmsMessage.createFromPdu(pdus);
                }
                StringBuffer content = new StringBuffer();//獲取簡訊內容
                StringBuffer phoneNumber = new StringBuffer();//獲取地址
                //分析簡訊具體引數
                for (SmsMessage temp : msg){
                    content.append(temp.getMessageBody());
                    phoneNumber.append(temp.getOriginatingAddress());
                }
                EventBus.getDefault().post(new EventInfo(EventType.SMS_Receiver, "電話號碼:"+phoneNumber.toString()+"  簡訊內容:"+content.toString()));
            }
        }
    }
}

public class TestActivity extends Activity{
    @InjectView(R.id.tv_content)
    TextView tvContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.inject(this);
    }

    public void onEventMainThread(EventInfo eventInfo) {
        if (eventInfo.type == EventType.SMS_Receiver) {
            tvContent.setText(eventInfo.obj+"..收到了");
        }
    }
}

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/white"
              android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/space_50dp"
        android:gravity="center"
        android:text="測試頁面"/>

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/blue"/>
</LinearLayout>