Android 安卓短信接收器不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1944102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android SMS receiver not working
提问by Jess
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.
我正在尝试编写一个简单的应用程序,尝试接收和处理 SMS 消息。我已经学习了几个教程,但我一无所获,当我向模拟器发送短信时,意图似乎永远不会被解雇。
Here is my intent:
这是我的意图:
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());
}
}
}
}
}
and here is my AndroidManifest.xml:
这是我的 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>
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 开发,但我想我的头脑(大部分)都被它包围了。在监视模拟器的 logcat 时,日志事件永远不会出现,调试断点也永远不会命中,所以我感觉它在我的意图过滤器中的某个地方。
I'm running this on Android 2.0.1.
我在 Android 2.0.1 上运行它。
采纳答案by Christopher Orr
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
.
除了Samuh 的答案(您需要对操作字符串进行对象比较,或者不进行比较),在您的清单文件中,您还拼错了SMS_RECEIVED
.
回答by Samuh
I think your manifest looks okay; the problem is with the line:
我认为你的清单看起来不错;问题出在这一行:
if (intent.getAction() == SMS_RECEIVED) {
I think it should be: intent.getAction().equals(ACTION)
我觉得应该是: intent.getAction().equals(ACTION)
Hope that helps..
希望有帮助..
回答by Chris Genly
The action name seems to require a capital "T" in "Telephony.
在“电话”中,动作名称似乎需要大写的“T”。
android.provider.Telephony.SMS_RECEIVED
android.provider.Telephony.SMS_RECEIVED
回答by naveen
For String
variables you should not compare with ==
symbol, you should compare with equals()
method
对于String
不应该用==
符号比较的变量,你应该用equals()
方法 比较
this is wrong
这是错误的
intent.getAction() == SMS_RECEIVED)
this is right
这是对的
intent.getAction().equals (SMS_RECEIVED))
回答by Christoph
Since telephony is deprecated (since 1.0 I think), this method doesn't work anymore. Check out this tutorial if you need a proper solution: http://mobiforge.com/developing/story/sms-messaging-android(Just wanted to note this, since I stumbled upon a lot of tutorials that don't work anymore)
由于电话已被弃用(我认为从 1.0 开始),此方法不再适用。如果您需要适当的解决方案,请查看本教程:http: //mobiforge.com/developing/story/sms-messaging-android(只是想注意这一点,因为我偶然发现了很多不再适用的教程)