gpt4 book ai didi

android - 广播接收器永远不会被调用

转载 作者:行者123 更新时间:2023-11-29 14:08:21 24 4
gpt4 key购买 nike

我正在尝试学习Broadcast Receivers 的工作原理,但我没有成功,我的接收器从未被调用过。你们能指出我正确的方向吗?

代码如下:

主.java

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent intent = new Intent(getApplicationContext(), OutgoingReceiver.class);
intent.setAction("com.javaGroup.broadcastReceiver.views.TEST");
sendBroadcast(intent);

}
}

OutgoingReceiver.java

public class OutgoingReceiver extends BroadcastReceiver {

public static final String CUSTOM_INTENT = "com.javaGroup.broadcastReceiver.views.TEST";

@Override
public void onReceive(Context context, Intent intent) {
Log.d("HIT OUTGOING-----------------------------------","");
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
context.sendBroadcast(i);
}
}

IncomingReceiver.java

public class IncomingReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(OutgoingReceiver.CUSTOM_INTENT)) {
Log.d("GOT THE INTENT-------------------------------","");
}
}
}

list .xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javaGroup.broadcastReceiver.views" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.javaGroup.broadcastReceiver.views.Main" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".OutgoingReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>

<receiver android:name=".IncomingReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.javaGroup.broadcastReceiver.views.TEST"></action>
</intent-filter>
</receiver>

</application>
</manifest>

我哪里错了?

最佳答案

您需要在 Manifest 中为您的接收者添加正确的权限。参见 list of permissions

关于android - 广播接收器永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5309612/

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