本文整理汇总了Java中android.net.ConnectivityManager类的典型用法代码示例。如果您正苦于以下问题:Java ConnectivityManager类的具体用法?Java ConnectivityManager怎么用?Java ConnectivityManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConnectivityManager类属于android.net包,在下文中一共展示了ConnectivityManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: observeNetworkConnectivity
import android.net.ConnectivityManager; //导入依赖的package包/类
@Override public Observable<Connectivity> observeNetworkConnectivity(final Context context) {
final String service = Context.CONNECTIVITY_SERVICE;
final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(service);
networkCallback = createNetworkCallback(context);
registerIdleReceiver(context);
final NetworkRequest request =
new NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
.build();
manager.registerNetworkCallback(request, networkCallback);
return connectivitySubject.toFlowable(BackpressureStrategy.LATEST).doOnCancel(new Action() {
@Override public void run() {
tryToUnregisterCallback(manager);
tryToUnregisterReceiver(context);
}
}).startWith(Connectivity.create(context)).distinctUntilChanged().toObservable();
}
示例2: onReceive
import android.net.ConnectivityManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
if(action == Intent.ACTION_BATTERY_CHANGED){
handleBattery(intent);
}else if(action == ConnectivityManager.CONNECTIVITY_ACTION){
AppModel.getNetworkState();
}else if(action == Intent.ACTION_SCREEN_ON){
AppModel.sDevScreenOff = false;
LogUtils.i("screen on");
}else if(action == Intent.ACTION_SCREEN_OFF){
AppModel.sDevScreenOff = true;
LogUtils.i("screen off");
}
}catch (Exception e){
LogUtils.e("onReceive error ", e);
}
}
示例3: getConnectedNetworks
import android.net.ConnectivityManager; //导入依赖的package包/类
private static List<NetworkInfo> getConnectedNetworks(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return null;
}
final List<NetworkInfo> list = new ArrayList<NetworkInfo>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
NetworkInfo[] networkInfoList = cm.getAllNetworkInfo();
if (networkInfoList != null) {
final int length = networkInfoList.length;
for (int i = 0; i < length; i++) {
if (networkInfoList[i].getState() == NetworkInfo.State.CONNECTED) {
list.add(networkInfoList[i]);
}
}
}
} else {
final Network[] networks = cm.getAllNetworks();
if (networks != null && networks.length > 0) {
NetworkInfo info;
for (Network network : networks) {
info = cm.getNetworkInfo(network);
if (info != null && info.getState() == NetworkInfo.State.CONNECTED) {
list.add(info);
}
}
}
}
return list;
}
示例4: isNetworkAvailable
import android.net.ConnectivityManager; //导入依赖的package包/类
public static boolean isNetworkAvailable(final Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
boolean state = activeNetworkInfo != null && activeNetworkInfo.isConnected();
if (!state) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(context.getString(R.string.internet_alert))
.setCancelable(false)
.setPositiveButton(context.getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
return state;
}
示例5: isNetworkAvailable
import android.net.ConnectivityManager; //导入依赖的package包/类
public boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
示例6: haveNetworkConnection
import android.net.ConnectivityManager; //导入依赖的package包/类
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
示例7: getAvailableNetworkInfo
import android.net.ConnectivityManager; //导入依赖的package包/类
public static NetworkInfo getAvailableNetworkInfo(Context context) {
NetworkInfo ni = null;
try {
ni = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
} catch (Exception e) {
e.printStackTrace();
}
return (ni == null || !ni.isAvailable()) ? null : ni;
}
示例8: checkPending
import android.net.ConnectivityManager; //导入依赖的package包/类
/**
* Checks if there is already a pending request for the user.
*
* @param name of user pending
* @return Boolean value. Whether if there is a pending request for the user.
* @throws Exception
*/
private boolean checkPending(String name) { // change to more specific exception later.
// Check if app is connected to a network.
ConnectivityManager cm = (ConnectivityManager) getActivity().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null == activeNetwork) {
Toast.makeText(getActivity(), "You are offline.", Toast.LENGTH_SHORT).show();
} else {
ElasticSearchUserController.GetUserTask getUserTask =
new ElasticSearchUserController.GetUserTask();
getUserTask.execute(name.toLowerCase());
try {
User user2 = getUserTask.get().get(0); // get first user from result
String currentUsername = CurrentUserSingleton.getInstance().getUser().getName();
return user2.getPending().contains(currentUsername);
} catch (Exception e) {
Log.d("Error", "Unable to get user from elastic search");
}
}
return false;
}
示例9: isTunnelNeeded
import android.net.ConnectivityManager; //导入依赖的package包/类
private boolean isTunnelNeeded(NetworkInfo info) {
if (info == null) {
Log.i("No connectivity: tunnel should be disabled");
return false;
}
String pref = mPrefs.getTunnelMode();
if (getString(R.string.tunnel_mode_entry_value_always).equals(pref)) {
return true;
}
if (info.getType() != ConnectivityManager.TYPE_WIFI
&& getString(R.string.tunnel_mode_entry_value_3G_only).equals(pref)) {
Log.i("need tunnel: 'no wifi' connection");
return true;
}
return false;
}
示例10: getNetworkType
import android.net.ConnectivityManager; //导入依赖的package包/类
/**
* 获取当前网络类型
*
* @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
*/
public static int getNetworkType(Context context) {
int netType = NetworkUtils.NET_TYPE_NOT;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int networkInfoType = networkInfo.getType();
if (networkInfoType == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = networkInfo.getExtraInfo();
if (!StringUtils.isEmpty(extraInfo)) {
if (extraInfo.toLowerCase(Locale.getDefault()).equals("cmnet")) {
netType = NetworkUtils.NET_TYPE_CM_NET;
} else {
netType = NetworkUtils.NET_TYPE_CM_WAP;
}
}
} else if (networkInfoType == ConnectivityManager.TYPE_WIFI) {
netType = NetworkUtils.NET_TYPE_WIFI;
}
return netType;
}
示例11: isConnected
import android.net.ConnectivityManager; //导入依赖的package包/类
/**
* 判断网络是否连接
*
* @param context context
* @return true is connected
*/
@Deprecated
public static boolean isConnected(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
示例12: registerLollipopNetworkReceiver
import android.net.ConnectivityManager; //导入依赖的package包/类
public static void registerLollipopNetworkReceiver(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.registerNetworkCallback(
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
boolean connected = false;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connected = cm.bindProcessToNetwork(network);
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//noinspection deprecation
connected = ConnectivityManager.setProcessDefaultNetwork(network);
}
if(connected) {
Customerly.get().__SOCKET__check();
}
}
});
}
}
示例13: testNetworkChangeNotifierIsOnline
import android.net.ConnectivityManager; //导入依赖的package包/类
@UiThreadTest
@MediumTest
@Feature({"Android-AppBase"})
public void testNetworkChangeNotifierIsOnline() throws InterruptedException {
Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
// For any connection type it should return true.
for (int i = ConnectivityManager.TYPE_MOBILE; i < ConnectivityManager.TYPE_VPN; i++) {
mConnectivityDelegate.setActiveNetworkExists(true);
mConnectivityDelegate.setNetworkType(i);
mReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
assertTrue(NetworkChangeNotifier.isOnline());
}
mConnectivityDelegate.setActiveNetworkExists(false);
mReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
assertFalse(NetworkChangeNotifier.isOnline());
}
示例14: isInternetOn
import android.net.ConnectivityManager; //导入依赖的package包/类
public boolean isInternetOn () {
// Log.e(TAG, "isInternetOn fired");
ConnectivityManager connec =
(ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
// Check for network connections
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
// if connected with internet
return true;
} else if (
connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
return false;
}
return false;
}
示例15: loadnet
import android.net.ConnectivityManager; //导入依赖的package包/类
private void loadnet() {
ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(CONNECTIVITY_SERVICE);
//获取系统的连接服务。
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//获取网络的连接情况。
if (networkInfo != null && networkInfo.isAvailable()) {
/* if (networkInfo.getType()==connectivityManager.TYPE_WIFI){
Toast.makeText(A.this,"网络已启动啦(WIFI)",Toast.LENGTH_SHORT).show();
}else if (networkInfo.getType()==connectivityManager.TYPE_MOBILE) {
Toast.makeText(A.this,"网络已启动啦(3G)",Toast.LENGTH_SHORT).show();
}*/
loadingLayout.setStatus(LoadingLayout.Success);//加载成功
} else {
loadingLayout.setStatus(LoadingLayout.Loading);
}
}