0

嗨 :) 我正在使用 android 中的 SMS。我的短信接收器类有问题。当我第一次在模拟器上运行我的应用程序时,它就像我编程的那样工作。但是每次我再次运行该应用程序时,它都无法正常工作,因为我将其更新为可以工作。我被困了最后两天。有人可以指导我或提供一些帮助。我的基本接收器类是:

public class SMSreceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();
    String Sender = null;
    String str = "";
    SmsMessage [] msgs = null;
    if(bundle != null)
    {
    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]);
            Sender = msgs[i].getOriginatingAddress();
            str = "SMS From: " + msgs[i].getOriginatingAddress();
            str += ":";
            str += msgs[i].getMessageBody().toString();
            str += "\n";
        }
        //Toast.makeText(context, str, Toast.LENGTH_LONG).show();
        Toast.makeText(context, Sender, Toast.LENGTH_LONG).show();
    }

}

在上面的代码中,我注释了显示消息的 toast,并尝试显示显示发件人号码的 toast。但它仍然显示新的味精文本。这很奇怪。

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pingpongsmsremote"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="12" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.pingpongsmsremote.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.pingpongsmsremote.SMSScheduler"
        android:label="@string/title_activity_smsscheduler" >
    </activity>
    <activity
        android:name="com.example.pingpongsmsremote.FilterSMS"
        android:label="@string/title_activity_filter_sms" >
    </activity>
    <activity
        android:name="com.example.pingpongsmsremote.SMSRemote"
        android:label="@string/title_activity_smsremote" >
    </activity>
    <activity
        android:name="com.example.pingpongsmsremote.SendSms"
        android:label="@string/title_activity_send_sms" >
    </activity>
</application>



</manifest>
4

1 回答 1

2

看起来您可能忘记注册接收器了?您的清单中需要这样一行:

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

要在完整的清单示例中查看它,如下所示:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pingpongsmsremote"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="12" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.pingpongsmsremote.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.pingpongsmsremote.SMSScheduler"
        android:label="@string/title_activity_smsscheduler" >
    </activity>
    <activity
        android:name="com.example.pingpongsmsremote.FilterSMS"
        android:label="@string/title_activity_filter_sms" >
    </activity>
    <activity
        android:name="com.example.pingpongsmsremote.SMSRemote"
        android:label="@string/title_activity_smsremote" >
    </activity>
    <activity
        android:name="com.example.pingpongsmsremote.SendSms"
        android:label="@string/title_activity_send_sms" >
    </activity>
    <receiver android:name="SMSreceiver" >
                <intent-filter>
                    <action android:name="android.provider.telephony.SMS_RECEIVED"/>
                </intent-filter>
    </receiver>
</application>
</manifest>
于 2013-05-03T13:51:58.100 回答