[英]onReceive never getting called
Below is my Java code and my XML code. 下面是我的Java代码和我的XML代码。 Can someone please tell me why my
onReceieve
method is never getting called. 有人可以告诉我为什么我的
onReceieve
方法永远不会被调用。
Java: Java的:
public class PopUPSMS extends Activity {
String RECEIVE_SMS = "RECEIVE_SMS";
private static final String LOG_TAG = "PopUPSMS";
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(LOG_TAG, "onCreate");
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "onReceive");
}
}, new IntentFilter(RECEIVE_SMS));
}
}
XML: XML:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smith.johnathan.phone"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PopUPSMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-sdk android:minSdkVersion="3" />
</manifest>
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "onReceive");
}
}, new IntentFilter(RECEIVE_SMS));
You have registered your broadcast receiver with the string "RECEIVE_SMS". 您已使用字符串“RECEIVE_SMS”注册了广播接收器。 The IntentFilter should be of a form of an action.
IntentFilter应该是一种动作形式。 With your declaration:
随你的声明:
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
you will have: 您将拥有:
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "onReceive");
}
}, new IntentFilter(ACTION));
You can have a look at the Api demos Manifest for the XML declaration 您可以查看XML声明的Api演示清单
You can't use receiver as an inner class. 您不能将接收器用作内部类。 Create a separate one.
创建一个单独的。
Don't you need to specify that you want to receive SMS in your manifest? 您是否需要指定要在清单中接收短信?
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".MyApp">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.