2

受信した SMS メッセージをリッスンしようとしていますが、onReceive メソッドが呼び出されません。このプログラムは android のバージョン 1.6 用にビルドされており、バージョン 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 に答える