我正在开发一个应用程序,我有几个活动,我必须注册一个BroadcastReceiver
应该检测,我们是否已连接(WiFi /移动),我可以做到这一点,如果我没有问题为测试活动做,但我不确定,我现在应该如何进行这样的架构
SplashActivity-> LoginActivity-> MainActivity *
LoginActivity和MainActivity有操作,需要Connectivity
,我的问题是
1)我必须单独为LoginActivity和MainActivity注册/取消注册BroadcastReceiver,或者如果我在LoginActivity中注册它并且当我从MainActivity退出时取消注册(从应用程序退出)
2) RegisterReceiver
/ UnregisterReceiver
操作通常在onResume
/ onPause
中实施,但在LoginActivity中我的Connectivity
明智的操作和MainActivity访问onCreate
中的此数据,因此,早于onResume
,我该如何克服这一点。
* P.S。根据此Connectivity
,以及我的连接性响应 - BroadcastReceiver
,我将能够转到MainActivity。
编辑: 我也多次收到每条消息的对话(移动/ WiFi /无互联网)
我的代码
在我的地图活动中看起来像这样
...
@Override
protected void onResume() {
super.onResume();
registerNetworkReceiver();
}
private void registerNetworkReceiver() {
if (networkChangeReceiver!= null) {
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkChangeReceiver, filter);
}
}
@Override
protected void onPause() {
super.onPause();
if (networkChangeReceiver != null)
unregisterReceiver(networkChangeReceiver);
}
...
我的BroadcastReceiver
public class NetworkChangeReceiver extends BroadcastReceiver {
private boolean firstCheckingConnection = true;
@Override
public void onReceive(final Context context, final Intent intent) {
NetworkUtils netUtils = new NetworkUtils(context);
// Avoid multiple callings
NetworkInfo netInfo = netUtils.getNetworkInfo();
if ( netInfo != null) {
if ( firstCheckingConnection ) {
firstCheckingConnection = false;
String statusString = netUtils.getConnectivityStatusString(context);
// Just Activities can show Dialogs/Windows, so, we must bridge with a dummy Activity!!!
Intent ad_activity_intent = new Intent(context, AlertDialogActivity.class);
ad_activity_intent.putExtra(Constants.DESCRIPTION, statusString);
ad_activity_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ad_activity_intent);
} else {
firstCheckingConnection = true;
}
}
}
}
NetworkUtils Class
public class NetworkUtils {
private static String ssid;
private static NetworkInfo activeNetwork ;
private Context context;
private ConnectivityManager cm;
public static final int TYPE_WIFI = 1;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_NOT_CONNECTED = 0;
public NetworkUtils(Context context){
this.context = context;
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetwork = cm.getActiveNetworkInfo();
}
public static int getConnectivityStatus(Context context) {
ssid = "";
int status = TYPE_NOT_CONNECTED;
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
status = TYPE_WIFI;
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiConnectionInfo = wifiManager.getConnectionInfo();
if (wifiConnectionInfo != null && !isBlank(wifiConnectionInfo.getSSID()))
ssid = wifiConnectionInfo.getSSID();
} else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
status = TYPE_MOBILE;
}
return status;
}
private static boolean isBlank(String ssid) {
return ( ssid == null || ssid.length()==0 );
}
public static String getConnectivityStatusString(Context context) {
int conn = getConnectivityStatus(context);
String status = "";
switch (conn) {
case TYPE_WIFI:
status = "Wifi enabled:["+ssid+"]";
break;
case TYPE_MOBILE:
status = "Mobile data enabled";
break;
case TYPE_NOT_CONNECTED:
status = "Not connected to Internet";
break;
}
return status;
}
public static NetworkInfo getNetworkInfo() {
return activeNetwork;
}
}