2

我寫了一個發送和接收短信成功的android。如何從特定電話號碼接收短信?

我想我的程序能夠從一個特殊號碼(「+ 9856874236」)接收短信。但是,如果短信來自任何其他號碼,它應該轉到電話的消息收件箱,而不是我的應用程序。

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.util.Log; 

    public class SmsReceiver extends BroadcastReceiver { 
     public String str = ""; 

     @Override 
     public void onReceive(Context context, Intent intent) { 

      Bundle bundle = intent.getExtras(); 
      Object messages[] = (Object[]) bundle.get("pdus"); 
      SmsMessage[] msgs = null; 

      if (bundle != null) 
      { 

       Object[] pdus = (Object[]) bundle.get("pdus"); 
       SmsMessage smsMessage[] = new SmsMessage[messages.length];  
       String msg_from=""; 
       for (int n = 0; n < messages.length; n++) 
       { 
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); 
         msg_from += msgs[n].getOriginatingAddress(); 
       } 

       String receivedMessage = smsMessage[0].getMessageBody().toString().toUpperCase(); 


       if(msg_from .equals("+989124236870")) 
       { 
       for (int n = 0; n < messages.length; n++) 
       { 

        smsMessage[n] = SmsMessage.createFromPdu((byte[]) pdus[n]); 
        str += "SMS from " + msgs[n].getOriginatingAddress(); 
        str += " :"; 
        //str += "sms az shomare makhsus"; 
        str += msgs[n].getMessageBody().toString(); 

        str += "\n"; 

       abortBroadcast(); 

       } 


       Intent act = new Intent(context, MainActivity.class); 
       act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       act.putExtra("message", str); 
       context.startActivity(act); 
      } 


      } 

     } 
    } 
+0

看來你有4個類似的問題打開。 1. http://stackoverflow.com/questions/13108337/how-to-receive-sms-only-from-one-specific-number-other-smss-go-to-phones-inbox 2. HTTP:/ /stackoverflow.com/questions/13097704/how-to-received-sms-from-special-number 3. http://stackoverflow.com/questions/13107161/filtering-received-sms-in-android 4.本一。 不酷。請不要這樣做。 –

+0

因爲我沒有得到我的問題的答案!!!!!!!!我沒有任何辦法!你能解決我的問題嗎? – Pariya

回答

0

爲什麼要在for循環中追加msg_from參數?

msg_from += msgs[n].getOriginatingAddress(); 

它不應該僅僅是:

msg_from[n] = msgs[n].getOriginatingAddress(); 

基本上具有用於msg_from [n]的一個數組並把每個起始地址到它爲每一個消息。 然後通過遍歷msg_from數組檢查每個消息的這個?

您可以將檢查原產裏面的for循環如下:

for (int n = 0; n < messages.length; n++) { 
      if(msg_from[n].equals("+9856874236")){ 
       smsMessage[n] = SmsMessage.createFromPdu((byte[]) pdus[n]); 
       str += "SMS from " + msg_from[n]; 
       str += " :"; 
       //str += "sms az shomare makhsus"; 
       str += msgs[n].getMessageBody().toString(); 

       str += "\n"; 

       abortBroadcast(); 
       }else{ 
       //Do something else 
       } 
      } 
+0

在if條件下,應該不會改變? 「if(msg_from .equals(」+ 9856874236「))」 – Pariya

+0

是的,它應該。我有我的答案更新爲: 如果(msg_from [N] .equals( 「+ 9856874236」)) – krishnang

+0

這個代碼didnt工作尚未!!!!!!!!!!!!!!!!!!!!! !1 – Pariya

0

使用該樣品並進行自定義:

import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.telephony.SmsMessage; 
import android.widget.Toast; 

public class SrvSmsListener extends Service { 

    private BroadcastReceiver IncomingSMSReceiver = new BroadcastReceiver() { 
     private static final String SMS_RECEIVED = 
       "android.provider.Telephony.SMS_RECEIVED"; 

     @Override 
     public void onReceive(Context _context, Intent _intent) { 
      if (_intent.getAction().equals(SMS_RECEIVED)) { 
       Bundle bundle = _intent.getExtras(); 
       if (bundle != null) { 
        Object[] pdus = (Object[]) bundle.get("pdus"); 

        SmsMessage[] messages = new SmsMessage[pdus.length]; 
        for (int i = 0; i < pdus.length; i++) 
         messages[i] = SmsMessage 
           .createFromPdu((byte[]) pdus[i]); 
        for (SmsMessage message : messages) { 
         String strPhoneNo = message.getOriginatingAddress(); 
         String msg = message.getMessageBody(); 

         if (msg.startsWith("my text")) 
         { 
          // this stops notifications to others 
          this.abortBroadcast(); 

         } 
        } 
       } 
      } 
     } 
    }; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; 
     IntentFilter filter = new IntentFilter(SMS_RECEIVED); 
     BroadcastReceiver receiver = IncomingSMSReceiver; 
     registerReceiver(receiver, filter); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onDestroy() { 
     if (IncomingSMSReceiver != null) 
     { 
      unregisterReceiver(IncomingSMSReceiver); 
     } 
    } 
} 
+0

這段代碼創建強制關閉!!!!!!!!!!它是錯誤!!!! – Pariya

+0

你有沒有把它添加到清單? – breceivemail

+0

我創建了新的項目,我把你的代碼放在class.java中,併發送類,我把我的代碼,併爲清單,我把以前的代碼,但它創建強制關閉時,在手機中接收短信! – Pariya

0

too.now我用這個代碼,我有這個問題它正常工作:

 `enter code here` 
     if (extras != null) { 
     // get array data from SMS 
     Object[] smsExtra = (Object[]) extras.get("pdus"); // "pdus" is the key 

     for (int i = 0; i < smsExtra.length; ++i) { 
      // get sms message 
      SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]); 
      // get content and number 
      String body = sms.getMessageBody(); 
      String address = sms.getOriginatingAddress(); 
      // create display message 

      if(address.contains("5558")){ 

      messages += "SMS from " + address + " :\n"; 
      messages += body + "\n"; 

     // notify new arriving message 
      Toast.makeText(context, messages, Toast.LENGTH_LONG).show(); 

     this.abortBroadcast(); 

    } 
    } 

,也不要忘了使用此代碼您的清單:

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

..... 
<receiver android:name=".SmsReceiver" android:exported="true" > 
<intent-filter android:priority="1"> 
<action android:name="android.provider.Telephony.SMS_RECEIVED" /> 

</intent-filter> 
</receiver> 

該程序只接收端口=「5558」(在仿真器中)並將其他信息存儲在收件箱中。

是(09124236870)自己的電話號碼?!

相關問題