广播接收器未调用Internet连接检查 [英] Broadcast receiver is not calling on internet connectivity check
问题描述
我正在尝试制作一个简单的应用程序,该应用程序将通知是否有可用的互联网连接,并且互联网连接发生更改。我在互联网上找到了一些解决方案,并尝试实施它们,但是不知何故,它无法正常工作。我已在清单文件中注册的广播接收器未调用网络连接更改。
清单
< uses-permission android:name = android.permission.INTERNET />
< uses-permission android:name = android.permission.ACCESS_WIFI_STATE />
< uses-permission android:name = android.permission.ACCESS_NETWORK_STATE />
<应用程序
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>
<接收器
android:name =。NetworkStateChangeReceiver>
<意图过滤器>
< action android:name = android.net.conn.CONNECTIVITY_CHANGE />
< / intent-filter>
< / receiver>
< / application>
广播接收器
package com.gdm.internetconnectivitycheck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
导入静态android.content.Context.CONNECTIVITY_SERVICE;
公共类NetworkStateChangeReceiver扩展了BroadcastReceiver {
public static final String NETWORK_AVAILABLE_ACTION = com.gdm.retailalfageek.NetworkAvailable;
public static final String IS_NETWORK_AVAILABLE = isNetworkAvailable;
@Override
public void onReceive(上下文上下文,目的意图){
意图networkStateIntent = new Intent(NETWORK_AVAILABLE_ACTION);
networkStateIntent.putExtra(IS_NETWORK_AVAILABLE,isConnectedToInternet(context));
LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);
Log.e(网络可用,在收到呼叫时);
}
私有布尔值isConnectedToInternet(上下文上下文){
try {
if(context!= null){
ConnectivityManager ConnectivityManager =(ConnectivityManager)上下文.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = ConnectivityManager.getActiveNetworkInfo();
return networkInfo!= null&& networkInfo.isConnected();
}
返回false;
} catch(Exception e){
Log.e(NetworkStateChangeReceiver.class.getName(),e.getMessage());
返回false;
}
}
}
主要活动
包com.gdm.internetconnectivitycheck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
导入静态com.gdm.internetconnectivitycheck.NetworkStateChangeReceiver.IS_NETWORK_AVAILABLE;
公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter(NetworkStateChangeReceiver.NETWORK_AVAILABLE_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context,Intent intent){
boolean isNetworkAvailable = intent.getBooleanExtra( IS_NETWORK_AVAILABLE,否);
字符串networkStatus = isNetworkAvailable? connected: disconnected;
Snackbar.make(findViewById(R.id.main_activity),网络状态: + networkStatus ,Snackbar.LENGTH_LONG).show();
}
},intentFilter);
}
}
面向Android 7.0(API级别24)及更高版本的应用必须在
$ b $广播后注册
b
registerReceiver(BroadcastReceiver,IntentFilter)
< blockquote>
在清单中声明接收者无效。
CONNECTIVITY_ACTION
从Android 8.0(API级别26)开始,
系统对清单声明的接收者施加了额外的限制。
如果您的应用程序的目标是API级别26或更高级别,则不能使用
清单来声明大多数隐式广播
(未专门针对您的应用程序的广播)的接收者。当用户正在积极使用
应用程序时,您仍然可以
使用上下文注册的接收者。
来自官方文档。
您需要在运行时从活动注册CONNECTIVITY_CHANGE操作。
使用 registerReceiver 。
IntentFilter filter = new IntentFilter();
filter.addAction( android.net.conn.CONNECTIVITY_CHANGE);
registerReceiver(new NetworkStateChangeReceiver(),filter);
别忘了注销。
I am trying to make a simple app which will notify if there is internet connection available or not on internet connectivity change. i have found some solution on internet and tries to implement them but somehow its not working. my broadcast receiver which i have registered on my manifest file is not calling on network connectivity change.
Manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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=".NetworkStateChangeReceiver">
<intent-filter >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
Broadcast Receiver
package com.gdm.internetconnectivitycheck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import static android.content.Context.CONNECTIVITY_SERVICE;
public class NetworkStateChangeReceiver extends BroadcastReceiver {
public static final String NETWORK_AVAILABLE_ACTION = "com.gdm.retailalfageek.NetworkAvailable";
public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";
@Override
public void onReceive(Context context, Intent intent) {
Intent networkStateIntent = new Intent(NETWORK_AVAILABLE_ACTION);
networkStateIntent.putExtra(IS_NETWORK_AVAILABLE, isConnectedToInternet(context));
LocalBroadcastManager.getInstance(context).sendBroadcast(networkStateIntent);
Log.e("Network Available ", "On receive called");
}
private boolean isConnectedToInternet(Context context) {
try {
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
return false;
} catch (Exception e) {
Log.e(NetworkStateChangeReceiver.class.getName(), e.getMessage());
return false;
}
}
}
Main Activity
package com.gdm.internetconnectivitycheck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import static com.gdm.internetconnectivitycheck.NetworkStateChangeReceiver.IS_NETWORK_AVAILABLE;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter(NetworkStateChangeReceiver.NETWORK_AVAILABLE_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false);
String networkStatus = isNetworkAvailable ? "connected" : "disconnected";
Snackbar.make(findViewById(R.id.main_activity), "Network Status: " + networkStatus, Snackbar.LENGTH_LONG).show();
}
}, intentFilter);
}
}
Apps targeting Android 7.0 (API level 24) and higher must register the following broadcasts with
registerReceiver(BroadcastReceiver,IntentFilter)
Declaring a receiver in the manifest does not work.
CONNECTIVITY_ACTION
Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers. If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically). You can still use a context-registered reciever when the user is actively using your app.
directly from official doc.
you need to register for CONNECTIVITY_CHANGE action at runtime from activity. using registerReceiver.
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(new NetworkStateChangeReceiver(), filter);
And don't forget to unregister.
这篇关于广播接收器未调用Internet连接检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!