2017-05-20 50 views
0
  1. SMSReceiver.java收到ListView項短信使用意向

    public class SMSReceiver extends BroadcastReceiver{ 
    public void onReceive(Context context, Intent intent) { 
    
    Bundle bundle = intent.getExtras(); 
    String str = ""; 
    if (bundle != null) { 
    
        Object [] pdus = (Object[])bundle.get("pdus"); 
    
        SmsMessage [] msgs = new SmsMessage[pdus.length]; 
        String [] lmsg = new String[2048]; 
        for (int i = 0; i < msgs.length; i++) { 
         msgs[i] = SmsMessage 
           .createFromPdu((byte[]) pdus[i]); 
         str += msgs[i].getOriginatingAddress() 
           + "\n " + 
           msgs[i].getMessageBody().toString() 
           +"\n"; 
        } 
        for (int j = 0; j < lmsg.length ; j++) { 
         lmsg[j] = str; 
        } Toast.makeText(context, str, Toast.LENGTH_LONG).show(); 
        } 
        } 
    } 
    

2.RedirectActivity

public class RedirectActivity extends AppCompatActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent intent = getIntent(); 
    intent.setClass(RedirectActivity.this, ParentActivity.class); 

    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(intent); 
    finish(); 
    } 
} 

我想從SMS資料ParentActivity。

  1. SMSReceiver:接收短信
  2. RedirectActivity:將消息傳遞給ParentActivity
  3. ParentActivity:由意向

從redirectActivity獲取數據,我SMSReceiver和RedirectActivity之間有問題。它不起作用。

+1

的[服務追趕短信詳情]可能的複製(http://stackoverflow.com/questions/43996446/catching-sms-details-in-service) – jagapathi

+0

@Hhhh請檢查我的答案.. –

回答

0

請嘗試這一個兄弟

public class IncomingSms extends BroadcastReceiver { 

    private static SmsListener mListener; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle data = intent.getExtras(); 

     Object[] pdus = (Object[]) data.get("pdus"); 

     for(int i=0;i<pdus.length;i++){ 
      SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]); 

      String sender = smsMessage.getDisplayOriginatingAddress(); 
      //You must check here if the sender is your provider and not another one with same text. 

      String messageBody = smsMessage.getMessageBody(); 

      //Pass on the text to our listener. 
      mListener.messageReceived(messageBody); 
     } 
    } 

    public static void bindListener(SmsListener listener) { 
     mListener = listener; 
    } 
} 

接口監聽

public interface SmsListener { 
    public void messageReceived(String messageText); 
} 

活動得到Mesage

IncomingSms.bindListener(new SmsListener() { 
     @Override 
     public void messageReceived(String messageText) { 
      Log.d("Text", messageText); 
      Toast.makeText(VerifyOTPActivity.this, "Message: " + messageText, Toast.LENGTH_LONG).show(); 


     } 
    }); 

不要忘了這個清單文件

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

<receiver android:name=".IncomingSms"> 
     <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
     </intent-filter> 
</receiver>