2011-06-14 4 views
0

안드로이드 프로그래밍에 익숙하지 않아 문제를 해결하는 데 도움이됩니다.Android SMS 수신기가 작동하지 않음

SMS를 수신하는 코드가 작동하지 않습니다. 자바 코드

package com.android.SMS; 


import android.os.Bundle; 

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

import android.widget.Toast; 

public class SMSReceiver extends BroadcastReceiver 
{ 

    public void onReceive(Context context, Intent intent) 
     { 
      //---get the SMS message passed in--- 
      Bundle bundle = intent.getExtras();   
      SmsMessage[] msgs = null; 
      String str = "";    
      if (bundle != null) 
      { 
       //---retrieve the SMS message received--- 
       Object[] pdus = (Object[]) bundle.get("pdus"); 
       msgs = new SmsMessage[pdus.length];    
       for (int i=0; i<msgs.length; i++){ 
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
        str += "SMS from " + msgs[i].getOriginatingAddress();      
        str += " :"; 
        str += msgs[i].getMessageBody().toString(); 
        str += "\n";   
       } 
       //---display the new SMS message--- 
       Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
      }       
     } 


} 

이 문제를 해결 도와주세요하면서

매니페스트 파일은

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

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

입니다. 나는 어제부터 이것에 집착하고 있으며 코드에 어떤 문제도 보지 못했습니다.

+0

"작동하지 않는다"는 의미에 대해 좀 더 구체적으로 설명해 주실 수 있습니까? 예외가 발생합니까? logcat 출력에 오류/경고가 있습니까? –

+0

에뮬레이터에서 테스트 중이십니까? – Azlam

+0

나는 비슷한 것을하고있다 !!! http://stackoverflow.com/questions/14452808/sending-and-receiving-mms-in-android – toobsco42

답변

0

이 줄을 삭제하십시오. 가져 오기 android.telephony.gsm.SmsMessage;

SmsMessage는 "android.telephony"에서 클래스 여야합니다.

이 당신을 도울 수 ... :)

1
package com.google.android; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.widget.Toast; 

public class SMSReceiver extends BroadcastReceiver {  
    @Override /** This line is important, as you have not overriden the original method*/ 
    public void onReceive(Context context, Intent intent) { 
     //---get the SMS message passed in--- 
     Bundle bundle = intent.getExtras();   
     SmsMessage[] msgs = null; 
     String str = "";    
     if (bundle != null) { 
      //---retrieve the SMS message received--- 
      Object[] pdus = (Object[]) bundle.get("pdus"); 
      msgs = new SmsMessage[pdus.length];    
      for (int i=0; i<msgs.length; i++) { 
       msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
       str += "SMS from " + msgs[i].getOriginatingAddress();      
       str += " :"; 
       str += msgs[i].getMessageBody().toString(); 
       str += "\n";   
      } 
      //---display the new SMS message--- 
      Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

은 내가 당신을 도울 바랍니다.

1

여기 제가 지금 저를 위해 일해 왔습니다. 제공하는 코드는 수신 문자 메시지를 차단하는 데 사용되지만 들어오는 메시지에 대해서만 경고하고 더 이상 처리하지 않는 영역 만 포함하도록 쉽게 수정할 수 있습니다.

,617 :


는 SmsReceiver.java

package com.android.SMS; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.widget.Toast; 

public class SmsReceiver extends BroadcastReceiver { 

public static int MSG_TPE=0; 
private String getAddress; 
public void onReceive(Context context, Intent intent) { 
    String MSG_TYPE=intent.getAction(); 
     if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) { 
      Toast received = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG); 
      received.show(); 

       Bundle bundle = intent.getExtras(); 
       Object messages[] = (Object[]) bundle.get("pdus"); 
       SmsMessage smsMessage[] = new SmsMessage[messages.length]; 
       for (int n = 0; n < messages.length; n++) { 
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); 
       } 

        getAddress = smsMessage[0].getOriginatingAddress(); 
        // Filter incoming messages 
        if(getAddress.equals("APPROVEDPHONENUMBER")) { 
         Toast approved = Toast.makeText(context,"Approved SMS from: " + smsMessage[0].getOriginatingAddress(), Toast.LENGTH_LONG); 
         approved.show(); 
          // Message is approved and let through 
        } else { 
         Toast blocked = Toast.makeText(context,"Blocked SMS from: " + smsMessage[0].getOriginatingAddress(), Toast.LENGTH_LONG); 
         blocked.show(); 
          // Message is blocked 
          abortBroadcast(); 
        } 
        // End filter 
         for(int i=0;i<8;i++) { 
          System.out.println("Blocking SMS"); 
         } 

     } 

} 

} 


이 들어오는 메시지를

if(MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) { 
     Toast received = Toast.makeText(context,"SMS Received: "+MSG_TYPE , Toast.LENGTH_LONG); 
     received.show(); 
} 



의 AndroidManifest.xml

PERMISSIONS를 검출 코드

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


신청 BLOCK :

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 


    <service android:name=".MyService" android:enabled="true"/> 
    <receiver android:name="SmsReceiver"> 
      <intent-filter android:priority="2147483647"> 
       <action android:name="android.provider.Telephony.SMS_SENT"/> 
      </intent-filter> 
    </receiver> 
    <service android:name=".MyServiceSentReceived" android:enabled="true"/> 
     <receiver android:name="SmsReceiver"> 
       <intent-filter android:priority="2147483645"> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
       </intent-filter> 
     </receiver> 

</application> 

그것은 위의 코드와 같이 기본 "응용 프로그램"블록의 내부 서비스와 수신기 블록을 배치하는 것이 중요합니다.

관련 문제