BroadcastReceiver没有被实例化

时间:2016-09-22 20:12:10

标签: android

我有一个没有被实例化或调用的BroadcastReceiver,对我所做的任何帮助都表示赞赏。

它应该响应wifi连接/断开事件但不是,并且它的超类构造函数甚至都没有被调用。

MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

广播接收器:

public class ConnectivityChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "action: " + intent.getAction());
        Log.v(TAG, "component: " + intent.getComponent());
    }
}

的AndroidManifest.xml:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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=".ConnectivityChangeReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

<action>名称设置不正确。它应该是:android.net.conn.CONNECTIVITY_CHANGE。请参阅here所述的“常数值”。