我需要一个广播接收器名称“ NetworkReciver.java”,该名称应在Internet连接或断开时执行。但这是执行。

清单权限

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


 <receiver android:name=".NetworkReciever" >
    <intent-filter>
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>


在MainActivity.java中请求权限

String[] permissionsRequired = new String[]{Manifest.permission.SET_ALARM,
     Manifest.permission.INTERNET,
     Manifest.permission.ACCESS_NETWORK_STATE,};
     // Since these are Normal Permissions
     ActivityCompat.requestPermissions(this, permissionsRequired, 1);


NetworkReciever.java:

public class NetworkReciever extends BroadcastReceiver
{

         @Override
         public void onReceive(Context context, Intent intent)
         {
                Log.i("KAMLESH", "Network REceiver Alarm Set in Hair Style");
         }
}

最佳答案

您必须像该registerReceiver(new NetworkReciever())一样注册此广播接收器的NetworkReciever;在任何活动中

关于android - 网络更改的广播接收器未执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51700739/

10-09 13:25