BroadcastReceiver 不起作用?

BroadcastReceiver doesn't work?

我的Manifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
        android:name="android.support.multidex.MultiDexApplication"
        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=".MyBroadCastRecieverInternet">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>
    </application>

这是我的服务代码块。

public class MyBroadCastRecieverInternet extends BroadcastReceiver {

    public void onReceive(final Context context, Intent intent) {
        Log.d("MyLog","Internet Reciever is on");
    }

}

有没有错误?我什么也没找到。它不起作用,我不知道为什么。

来自docs

Apps targeting Android 7.0 (API level 24) and higher do not receive CONNECTIVITY_ACTION broadcasts if they declare their broadcast receiver in the manifest. Apps will still receive CONNECTIVITY_ACTION broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.

CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

您可以将 targetSdk 降级到 23 或像这样使用动态广播接收器:

public class ConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        boolean isConnected = ConnectionManager.isConnectedToInternet(context);

        if (!isConnected) {
            Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show();
        }
    }
}

主要活动:

public class MainActivity extends AppCompatActivity {

    private Context mContext = this;

    private ConnectivityReceiver mConnectivityReceiver;

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

        mConnectivityReceiver = new ConnectivityReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();

        registerReceiver(mConnectivityReceiver, new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION
        ));
    }

    @Override
    protected void onPause() {
        unregisterReceiver(mConnectivityReceiver);

        super.onPause();
    }
}

你可以试试这个方法。您不需要为您的广播接收器制作一个全新的 class,但您可以像这样在 Main Activity 中使用它:

BroadcastReceiver receiveLocationReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Your custom action
    }

};

IntentFilter receiveLocationFilter = new IntentFilter();
receiveLocationFilter.addAction("android.intent.RECEIVE_LOCATION");

onStart中注册接收器:

registerReceiver(receiveLocationReceiver, receiveLocationFilter);

onStop取消注册:

unregisterReceiver(receiveLocationReceiver);

然后当你需要发送广播时,你只需要:

Intent sendBroadcastIntent = new Intent("android.intent.RECEIVE_LOCATION");
sendBroadcast(sendBroadcastIntent);