2014-01-08 4 views
-1

다음 작업으로 귀하의 조언이 필요합니다. 중요한 것은 내 앱이있는 기기가이 번호의 SMS에만 반응하고 다른 번호는 표준 또는 다른 타사 앱과 함께 잡히도록 특정 휴대 전화 번호로 앱을 유지해야한다는 것입니다. SMS에는 특수 앱을 사용하여 디코딩 할 수있는 특정 데이터가 포함되어 있어야하기 때문에 필요합니다.특정 앱에서 특정 발신자의 SMS를받는 방법은 무엇입니까?

그래서 내가 그것을 알아야하거나 야생을 검사해야합니다. :)

항상 감사드립니다.

+0

u가 SMS를 읽을 수있게되면 전화 번호 또는 원하는 모든 것으로 필터링 할 수 있습니다. 그냥 먼저 SMS를 받으십시오. http://andro-source.blogspot.com/2013/02/sms-reading-in-android-programmatically.html – Daler

+1

Android 4.4에서는 원하는대로 할 수 없습니다. * 모든 * SMS 메시지는 사용자가 선택한 기본 SMS 클라이언트로 전달됩니다. – CommonsWare

+0

@Daler @CommonsWare 링크 –

답변

1

이 작업을 수행하기 위해, 당신은이 라인을 따라 뭔가를해야합니다 : 매니페스트에

  1. 추가 필요한 권한. 아마 RECEIVE_SMS
  2. 이 (당신의 응용 프로그램에 해당되는 경우에만) 메시지를 소비 선택하거나 주식 SMS 앱이를 데리러 수 있도록 수신기 내부에 SMS BroadcastReceiver
  3. 핸들 들어오는 SMS 년대를 만듭니다. 다음 코드의

아무도 시험하지, 더 출발점 :

매니페스트

<uses-permission android:name="android.permission.RECEIVE_SMS"/> 
<receiver android:name=".YOURSMSReceiver"> 
    <intent-filter android:priority="SOMENUMBER"> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
    </intent-filter> 
</receiver> 

YOURSMSReceiver 내가 말했듯

public class YOURSMSReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceiver(Context context, Intent intent) 
    { 
     boolean bConsumeSms = false;   // flag to consume the sms so stock app doesn't pick it up 

     String data = ""; 
     if(intent.getAction().equals(android.provider.Telephony.SMS_RECEIVED)) 
     { 
      Bundle bundlePdus = intent.getExtras(); 
      Object[] pdus = (Object[])bundlePdus.get("pdus"); 
      SmsMessage messages = SmsMessage.createFromPdu((byte[])pdus[0]); 
      if(messages.getOriginatingAddress() == YOURCELLNUMBER) 
      { 
        // do something with the content. 
        // held in: messages.getMessageBody() 
        data = messages.getMessageBody(); // your custom content 

        // consume this sms 
        bCOnsume = true; 
      } 

      // in order to consume the message, we have to use abortBroadcast(). But only if we've 
      // processed it first. If not, then the stock app, should be allowed to pick it up 
      if(bConsume) 
      { 
        abortBroadcast(); 
      } 
     } 
    } 
} 

그 완전히 테스트되지는 않았지만 이론적으로는 필요가있다. 또한 pdu 배열의 크기에 기반하여 여러 가지 메시지를 처리해야합니다. 행운을 빕니다!

CommonsWare는 4.4 안드로이드 OS가 모든 SMS를 관계없이 전달한다고 지적했습니다. 나는 이것을 알지 못했다.

+0

그런 설명을 해주셔서 감사합니다. –

관련 문제