我一直困惑不解这其中最近,前2.3.5这个似乎很好地工作(我的同事设备)。但是对矿井现在从来没有触发。
I've been puzzling over this one recently, prior to 2.3.5 this seems to work fine (on my colleagues devices). However on mine it now never triggers.
我已经剥离了code右后卫,并提出了一个非常简单的测试应用程序,看看发生了什么事情。基本上的onReceive code没有出现过触发,尽管亚洲开发银行/ logcat中似乎显示BroadcastReveiver名册确实发生。
I've stripped the code right back and made a very simple test application to see what's going on. Basically the onReceive code doesn't ever appear to trigger, even though adb/logcat does seem to show the register of the BroadcastReveiver does take place.
下面是简单的code,我走了:
Here's the simple code I've gone for:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcasttech.testsmsreceive"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".TestSMSReceiveActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".mysmstestcall">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
然后:
package com.broadcasttech.testsmsreceive;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
public class TestSMSReceiveActivity extends Activity {
private BroadcastReceiver receiver;
private static final String TAG = "TestSMSApp";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, " App has started up");
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
Log.i(TAG, " Filter SMS_RECEIVED has been added");
//Extends BroadcastReceiver
receiver = new mysmstestcall();
registerReceiver(receiver,filter);
Log.i(TAG, " registerReceiver sorted");
}
//Also, to save headaches later
@Override
protected void onDestroy() {
Log.i(TAG, " unregistering Receiver");
unregisterReceiver(receiver);
Log.i(TAG, " done");
}
}
最后
package com.broadcasttech.testsmsreceive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class mysmstestcall extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "TestSMSApp";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
if (intent.getAction() == SMS_RECEIVED) {
//any action you want here..
Log.i(TAG, "SMS received has triggered");
}
}
}
所以这是一个相当简单的应用程序,应该只是记录并告诉我的BroadcastReceiver被触发时,但它只是不会在所有火。
So it is a fairly simple app that should just log and tell me when the BroadcastReceiver is triggered, but it just won't fire at all.
任何人都可以提出什么是错,我已经检查各种教程,检查我所知IceCreamSandwich是不同的,但试图整合这些修正过,这不有所作为无论是。
Can anyone suggest whats wrong, I've checked various tutorials, checked as I know IceCreamSandwich is different, but tried to incorporate those fixes too and this doesn't make a difference either.
在此先感谢!
主要的问题是,这条线在mysmstestcall是错误的:
The main problem is that this line in "mysmstestcall" is wrong:
如果(intent.getAction()== SMS_RECEIVED)
应改为这样的:
if (intent.getAction().equals(SMS_RECEIVED))