Android接收器短信不起作用

时间:2014-03-18 00:02:24

标签: java android sms telephonymanager

我想阅读传入的短信..我创建了这个类来做到这一点:

public class smsRec extends BroadcastReceiver {
    private static final String LOG_TAG = "SMSApp";

    /* package */ static final String ACTION =
            "android.provider.Telephony.SMS_RECEIVED";

    // @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            // if(message starts with SMStretcher recognize BYTE)
            StringBuilder sb = new StringBuilder();

            /* The SMS-Messages are 'hiding' within the extras of the Intent. */
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                /* Get all messages contained in the Intent*/
                android.telephony.SmsMessage[] messages = 
                    Telephony.Sms.Intents.getMessagesFromIntent(intent);

                /* Feed the StringBuilder with all Messages found. */
                for (android.telephony.SmsMessage currentMessage : messages){
                    sb.append("Received compressed SMSnFrom: ");
                    /* Sender-Number */
                    sb.append(currentMessage.getDisplayOriginatingAddress());
                    sb.append("n----Message----n");
                    /* Actual Message-Content */
                    sb.append(currentMessage.getDisplayMessageBody());
                }
            }
            /* Logger Debug-Output */
            Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);

            /* Show the Notification containing the Message. */ 
            Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();

        }
    }

    private void appendData(StringBuilder buf, String key, String value) {
        buf.append(", ");
        buf.append(key);
        buf.append('=');
        buf.append(value);
    }


}

并在清单中:

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

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

但我得到一个错误,logcat是:

03-18 01:00:47.212: E/AndroidRuntime(13808): FATAL EXCEPTION: main
03-18 01:00:47.212: E/AndroidRuntime(13808): Process: com.dd.speech, PID: 13808
03-18 01:00:47.212: E/AndroidRuntime(13808): java.lang.RuntimeException: Unable to instantiate receiver com.dd.speech.SMSReceiver: java.lang.ClassNotFoundException: Didn't find class "com.dd.speech.SMSReceiver" on path: DexPathList[[zip file "/data/app/com.dd.speech-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.dd.speech-2, /vendor/lib, /system/lib]]
03-18 01:00:47.212: E/AndroidRuntime(13808):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2420)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at android.app.ActivityThread.access$1700(ActivityThread.java:145)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at android.os.Looper.loop(Looper.java:136)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at android.app.ActivityThread.main(ActivityThread.java:5081)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at java.lang.reflect.Method.invoke(Native Method)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-18 01:00:47.212: E/AndroidRuntime(13808): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.dd.speech.SMSReceiver" on path: DexPathList[[zip file "/data/app/com.dd.speech-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.dd.speech-2, /vendor/lib, /system/lib]]
03-18 01:00:47.212: E/AndroidRuntime(13808):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
03-18 01:00:47.212: E/AndroidRuntime(13808):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2415)
03-18 01:00:47.212: E/AndroidRuntime(13808):    ... 8 more
03-18 01:00:47.212: E/AndroidRuntime(13808):    Suppressed: java.lang.ClassNotFoundException: com.dd.speech.SMSReceiver
03-18 01:00:47.212: E/AndroidRuntime(13808):        at java.lang.Class.classForName(Native Method)
03-18 01:00:47.212: E/AndroidRuntime(13808):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
03-18 01:00:47.212: E/AndroidRuntime(13808):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
03-18 01:00:47.212: E/AndroidRuntime(13808):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
03-18 01:00:47.212: E/AndroidRuntime(13808):        ... 10 more
03-18 01:00:47.212: E/AndroidRuntime(13808):    Caused by: java.lang.NoClassDefFoundError: Class "Lcom/dd/speech/SMSReceiver;" not found
03-18 01:00:47.212: E/AndroidRuntime(13808):        ... 14 more

我无法找到问题

1 个答案:

答案 0 :(得分:3)

java和manifest中的类名必须匹配。现在一个叫做SMSReceiver,另一个是smsRec