2

我正在尝试收听收到的 SMS 消息,但永远不会调用 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>
4

1 回答 1

0

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

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
于 2012-11-17T17:39:25.940 回答