gpt4 book ai didi

java - onReceive 永远不会被调用

转载 作者:行者123 更新时间:2023-12-02 08:24:19 27 4
gpt4 key购买 nike

下面是我的 Java 代码和 XML 代码。有人可以告诉我为什么我的 onReceieve 方法永远不会被调用吗?

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:

<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));

您已使用字符串“RECEIVE_SMS”注册广播接收器。 IntentFilter 应该是一个 Action 的形式。附上您的声明:

static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

你将拥有:

registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "onReceive");
}
}, new IntentFilter(ACTION));

您可以查看Api demos Manifest用于 XML 声明

关于java - onReceive 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4854708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com