Android的手机短信接收器不工作 [英] Android SMS receiver not working
问题描述
我想编写一个简单的应用程序试图接收SMS消息和处理它们。我跟着几个教程,但我越来越行不通,当我发送一个短信到模拟器,其目的似乎永远不会被解雇。
I'm trying to write a simple application that attempts to receive SMS messages and handle them. I've followed several tutorials but I'm getting nowhere, when I send a SMS to the emulator, the Intent never seems to get fired.
下面是我的意图:
package com.neocodenetworks.smsfwd;
import android.content.*;
import android.os.Bundle;
import android.telephony.*;
import android.util.Log;
public class SmsReciever extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "smsfwd";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
if (intent.getAction() == SMS_RECEIVED) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
NetComm.SendMessage("me", messages[0].getOriginatingAddress(), messages[0].getMessageBody());
}
}
}
}
}
这里是我的AndroidManifest.xml:
and here is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.neocodenetworks.smsfwd"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<receiver android:name=".SmsReciever">
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECIEVED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="6" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
我真的AP preciate有什么错一些指导。我刚开始进入Android的发展,但我认为我有我的头上缠(大部分)它。在监测模拟器的logcat,日志事件永远不会来了,调试断点永远不会打,所以我有一种感觉它在我的意图过滤器的地方。
I'd really appreciate some guidance with what's going wrong. I'm just getting into Android development but I think I have my head wrapped around (most of) it. While monitoring the emulator's logcat, the log events never come up, and debugging breakpoints are never hit, so I have a feeling it's somewhere in my intent filter.
我在Android 2.0.1运行此。
I'm running this on Android 2.0.1.
推荐答案
除了 Samuh的回答(你需要做的动作字符串对象比较,或者只是做没有可比性),在你的清单文件,你有拼写错误 SMS_RECEIVED
。
In addition to Samuh's answer (you need to do an object comparison on the action string, or just do no comparison), in your manifest file you have misspelled SMS_RECEIVED
.
这篇关于Android的手机短信接收器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!