2015-07-05 24 views

回答

2

你需要創建一個BroadcastReceiver這將當短信received.In的BroadCastReceiverOnReceive編寫代碼來調用retrive短信發送方號碼和比較的數量並基於該任務。

public class ReceiveSMS extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    final Bundle bundle = intent.getExtras(); 
     if (bundle != null) { 

      final Object[] pdusObj = (Object[]) bundle.get("pdus"); 

      for (int i = 0; i < pdusObj.length; i++) { 

       SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); 
       String phoneNumber = currentMessage.getDisplayOriginatingAddress(); 
       String message = currentMessage.getDisplayMessageBody(); 
       Log.v("ranjapp", phoneNumber + " " + message); 
       if (phoneNumber.equals("+91xxxxxx"))) { 
        //do something 
       }else{ 
        //do nothing 
       } 
      } 
     } 
    } 
} 

在AndroidManifest.xml你需要添加:

  1. ** <application>標籤內意圖過濾器的接收器

    <receiver 
        android:name=".ReceiveSMS" 
        android:exported="true" 
        android:permission="android.permission.BROADCAST_SMS"> 
        <intent-filter> 
         <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
         <action android:name="android.provider.Telephony.SMS_DELIVER" /> 
        </intent-filter> 
    </receiver> 
    
  2. 權限:

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <uses-permission android:name="android.permission.READ_SMS"/>

+0

,但我想這樣做,對於短信不打電話 –

+0

Np..I編輯我的答案。這從你的問題是不可理解的 – Psypher

+0

更新了我對SMS的回答.. – Psypher

相關問題