我一直在试图让这个计划工作,但至今无运气。我找不到我在哪里做错了。我不知道是否有什么毛病code,或调试。
I have been trying to get this program to work but so far having no luck. I cannot find where I am doing wrong. I'm not sure if there's something wrong with the code, or debugging.
我想,如果一个新的短信到达通知。
I'm trying to be notified if a new SMS arrives.
下面是我的程序:
package Technicaljar.SMSBroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SMSBroadcastReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";
@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());
}
}
}
}
}
和清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Technicaljar.SMSBroadcastReceiver"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" >
<receiver android:name=".SMSBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
我通过Telnet发送短信,我看不到任何意图在logcat中收到的邮件。下面是安装的时候我logcat中。
I am sending SMS through Telnet, and I cannot see any Intent received messages in the logcat. Here is my logcat from the time of installation.
D/AndroidRuntime( 478):
D/AndroidRuntime( 478): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime( 478): CheckJNI is ON
D/AndroidRuntime( 478): --- registering native functions ---
D/AndroidRuntime( 478): Shutting down VM
D/dalvikvm( 478): Debugger has detached; object registry had 1 entries
I/AndroidRuntime( 478): NOTE: attach of thread 'Binder Thread #3' failed
D/Mms:app ( 220): getSmsNewMessageNotificationInfo: count=14, first addr=12345, thread_id=4
D/dalvikvm( 151): GC_EXPLICIT freed 391 objects / 22552 bytes in 65ms
D/dalvikvm( 220): GC_EXPLICIT freed 926 objects / 44840 bytes in 73ms
所以,短信似乎是由仿真器接收,但貌似没有意图的发射。 我在做什么错在这里?安装完成后,我必须以某种方式启动这个接收器? 因为当我安装,我得到
So the SMS seems to be received by the emulator, but looks like the no intents are firing. What am I doing wrong here? After installing, do I have to somehow 'start' this receiver? Because when I install, I get
[2010-11-07 21:24:41 - SMSBroadcastReceiver] No Launcher activity found!
[2010-11-07 21:24:41 - SMSBroadcastReceiver] The launch will only sync the application package on the device!
所以我想知道如果有什么地方错在这里。
So I'm wondering if something's wrong here.
android.provider.Telephony.SMS_RECEIVED
有资本 T
,而你的清单中没有。
android.provider.Telephony.SMS_RECEIVED
has a capital T
, and yours in the manifest does not.
请记住,这个意图
动作不记录在案。
Please bear in mind that this Intent
action is not documented.