无法在Android上接收短信(流程很糟糕)

时间:2011-10-27 04:13:43

标签: android sms broadcastreceiver

我尝试了很多(如果不是全部......)接收短信示例,但在调用onReceive()之前总是失败。

CatLog:

10-26 20:05:30.990: INFO/System.out(2714): INFO: Received message
10-26 20:05:30.998: INFO/System.out(2714): INFO: Message body: Lmjgk
10-26 20:05:31.021: WARN/ActivityManager(1317): Unable to launch app org.apache.sms/10166 for broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) }: process is bad
10-26 20:05:31.021: WARN/ActivityManager(1317): finishReceiver called but none active

当与另一个BroadcastReceiver android.intent.action.PHONE_STATE一起实现时,后者被调用,并且在SMS失败时工作得非常好。

为了简化它,我刚刚创建了hello示例并更新了清单和SMS BroadcastReceiver的一个文件。

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byp.sms" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloActivity" 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="com.byp.sms.SMSReceiver">
            <intent-filter>
                <action android:name="android.provider.telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
</application>

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

BroadcastReceiver

    package com.byp.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class SMSReceiver extends BroadcastReceiver {

    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("SMSReceiver", "onReceive"); // <<=== It does not even get here....
        if (intent != null && intent.getAction() != null
                && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
            Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
            Log.d("SMSReceiver", "SMSReceived " + pduArray.toString());
        }
        Log.d("SMSReceiver", "onReceive...Done");
      }
    }

Hello活动:对生成的代码没有任何更改

package com.byp.sms;

import android.app.Activity;
import android.os.Bundle;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

我从设备中清除所有相关应用程序,卸载它,重新启动设备等等没有帮助。

非常感谢任何建议或提示!

由于

1 个答案:

答案 0 :(得分:3)

如果要在AndroidManifest文件中注册,则需要将Receiver设置为静态。 如果接收器不是静态的,那么你需要通过代码注册。请做上面的事情,它会显示一些积极的结果。