Android SMS Receiver无法正常工作

时间:2011-06-14 05:59:22

标签: android

我是Android编程新手,请帮我解决问题。

我接收短信的代码无效。

清单文件是

    

    <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>

而java代码

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();
            }                         
        }


}

请帮我解决问题。我从昨天开始就陷入困境,我在代码中没有看到任何问题。

5 个答案:

答案 0 :(得分: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 :(得分:1)

这是我目前为我工作的内容。 我提供的代码用于阻止传入的文本消息,但您可以轻松地将其修改为仅包含仅向您发送传入消息的区域,并且不再进一步处理它们。


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中

权限:

<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" />


应用程序块:

<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>

将服务和接收器块放在主“应用程序”块中非常重要,如上面的代码所示。

答案 2 :(得分:0)

删除此行。 import android.telephony.gsm.SmsMessage;

请确保SmsMessage必须在“android.telephony”下进行分类。

这可能会对你有所帮助...... :)

答案 3 :(得分:0)

您可能需要向接收者添加BROADCAST_SMS权限

<receiver android:name=".SmsReceiver" android:enabled="true"
    android:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="2147483647">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

答案 4 :(得分:0)

确保您的应用对设备/仿真器具有权限:

在设备/仿真器上转到... 设置->隐私->权限管理器->短信

确保您的应用在允许列表中。如果在拒绝列表中,请单击它,然后选择“允许”

我转了一圈又一个小时,想知道为什么直到我意识到这一点才调用我的代码。