首页 > 解决方案 > BroadcastReceiver 没有像我预期的那样工作

问题描述

我刚刚编写了这个小应用程序,以在每次更改飞行模式时显示一个小的 Toast。它不起作用

package com.example.android.broadcastreceiverexample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import static android.widget.Toast.*;

public class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent){
    makeText(context, "Intent detected", LENGTH_LONG).show();
}
}

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android.broadcastreceiverexample">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <receiver android:name=".MyReceiver" android:exported="true">
        <intent-filter tools:ignore="ExtraText">
            <action android:name="android.intent.action.AIRPLANE_MODE></action>
        </intent-filter>
    </receiver>

</application>

</manifest>

这里有什么我在概念上或代码中遗漏的东西吗?

标签: javaandroidandroid-studiobroadcastreceiverandroid-manifest

解决方案


推荐阅读