BroadcastReceiver不起作用?

时间:2017-10-01 10:55:47

标签: android service broadcastreceiver android-manifest

我的Manifest.xml

T

是我的服务代码块。

<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>

有什么错吗?我没找到任何东西。它没有用,我也不知道为什么。

2 个答案:

答案 0 :(得分:2)

来自docs

  

针对Android 7.0(API级别24)及更高版本的应用无法接收   如果CONNECTIVITY_ACTION宣布他们的广播,则广播   清单中的接收者。应用仍会收到CONNECTIVITY_ACTION   广播,如果他们注册他们的BroadcastReceiver   Context.registerReceiver()和该上下文仍然有效。

     

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();
        }
    }
}

MainActivity:

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();
    }
}

答案 1 :(得分:0)

您可以尝试这种方法。您不需要为您的广播接收器创建一个全新的课程,但您可以在主要活动中使用它,如下所示:

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);