BroadcastReceiver onReceive事件不会触发

时间:2012-11-17 17:35:35

标签: android broadcastreceiver

我正在尝试收听收到的短信,但永远不会调用onReceive方法。 该程序是为1.6版本的android而构建的,它在2.3.7版本中运行时没有错误,但我之前说过的事件并不会触发。

任何提示?

// SMSListener CODE

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

public class SMSListener extends BroadcastReceiver {

    protected String smsBody = "";
    public static final String SMS_EXTRA_NAME = "pdus";
    protected SQLEventDataSource events;

    @Override
    public void onReceive(Context context, Intent intent) {
        // Get SMS map from Intent
        Bundle extras = intent.getExtras();
        Toast.makeText( context, "O evento onReceive foi lançado", Toast.LENGTH_SHORT ).show();
        String messages = "";

        if ( extras != null )
        {
            // Get received SMS array
            Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );

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

                String body = sms.getMessageBody().toString();

                // check if it's a configuration message
                if (body.substring(0, 5) == "SMED@") {
                    String[] configs = body.split("@"); 
                    events.addEventConfiguration(configs[1], configs[2], configs[3]);
                }

            }

            // Display SMS message
            Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
        }
    }
    private void setSmsBody(String smsBody) {
        this.smsBody = smsBody;
    }

    public String getSMSBody() {
        // TODO Auto-generated method stub
        return this.smsBody;
    }
}

// AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pt.ipbeja.estig.smed"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="1" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.provider.Telephony.SMS_RECEIVED" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".SMEDGPSActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

    </application>


</manifest>

1 个答案:

答案 0 :(得分:0)

假设你真的粘贴了你的清单,它包含所需权限的拼写错误。它目前错误地列出了动作过滤器字符串(android.provider.Telephony.SMS_RECEIVED)而不是实际权限:

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