【发布时间】:2019-03-05 07:52:49
【问题描述】:
我正在学习 Broadcastreceiver。我在清单文件中声明了接收者,并且有类 MyBroadcastReceiver 来接收意图,但我无法接收任何意图。 onRecive() 函数永远不会被调用,例如当飞行模式改变或耳机插入或拔出时。以下是清单代码。
<receiver
android:name=".MyBroadCastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
<action android:name="android.intent.action.HEADSET_PLUG" />
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</receiver>
以下是 BroadCastReceiver 类的代码。
public class MyBroadCastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
StringBuilder sb = new StringBuilder();
sb.append("Action: " + intent.getAction() + "\n");
sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
String log = sb.toString();
Log.d(TAG, log);
Toast.makeText(context, log, Toast.LENGTH_LONG).show();
}
}
【问题讨论】:
-
需要更多逻辑,例如您如何启动服务
-
@AravindV 我没有任何服务,我只想在更改飞行模式或插入或拔出耳机时接收广播。