public boolean isOnline(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); //should check null because in airplane mode it will be null return (netInfo != null && netInfo.isConnected()); }
/** * 获取活动网路信息 * * @param context 上下文 * @return NetworkInfo */ private static NetworkInfo getActiveNetworkInfo(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo(); }
static View inflate(Context applicationContext, int layoutId) { LayoutInflater inflate = (LayoutInflater) applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); return inflate.inflate(layoutId, null); }
super(context, resource, textViewResourceId, objects); mResourceId = resource; mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = mLayoutInflater.inflate(mResourceId, parent, false); holder = new ViewHolder(); holder.name = (TextView)view.findViewById(R.id.text); holder.radioBtn = (RadioButton)view.findViewById(R.id.radioButton1); view.setTag(holder); }else{ holder = (ViewHolder)view.getTag();
AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = getApplicationContext(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello, this is a custom dialog!"); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create();
public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.carousel_gallery_li, null); } else { view = convertView; } final ImageView imageView = (ImageView) view.findViewById(R.id.image); Bitmap image = null; try { InputStream bitmap = mContext.getAssets().open(PLACEHOLDER_FILE); image = BitmapFactory.decodeStream(bitmap); } catch (IOException exception) { Log.e(TAG, "An error occurred when you have tried to open the file: "+ PLACEHOLDER_FILE, exception); } imageView.setImageBitmap(image); return view; } }
@SuppressLint("NewApi") public RoundCornerProgressBar(Context context, AttributeSet attrs) { super(context, attrs); if (!isInEditMode()) { isProgressBarCreated = false; isProgressSetBeforeDraw = false; isMaxProgressSetBeforeDraw = false; isBackgroundColorSetBeforeDraw = false; isProgressColorSetBeforeDraw = false; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.round_corner_layout, this); setup(context, attrs); isProgressBarCreated = true; } else { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { setBackground(new ColorDrawable(Color.parseColor("#CCCCCC"))); } else { setBackgroundColor(Color.parseColor("#CCCCCC")); } setGravity(Gravity.CENTER); TextView tv = new TextView(context); tv.setText("RoundCornerProgressBar"); addView(tv); } }
/** * Protected constructor that is overridden by the SuperActivityToast class. */ protected SuperToast(@NonNull Context context, @NonNull Style style, @Style.Type int type) { this.mContext = context; this.mStyle = style; this.mStyle.type = type; final LayoutInflater layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mView = onCreateView(context, layoutInflater, type); this.mTextView = (TextView) this.mView.findViewById(R.id.message); }
@Test public void testNetworkStateUnmeteredWifi() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.UNMETERED); }
public static boolean isWifiConnected(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiNetworkInfo.isConnected()) { return true; } return false; }
/** * Checks if the device has any active internet connection. * * @return true device with internet connection, otherwise false. */ private boolean isThereInternetConnection() { boolean isConnected; ConnectivityManager connectivityManager = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); isConnected = (networkInfo != null && networkInfo.isConnectedOrConnecting()); return isConnected; } }
Integer layout = (Integer) convertView.getTag(AQuery.TAG_LAYOUT); if(layout != null && layout.intValue() == layoutId){ return convertView; inflater = act.getLayoutInflater(); }else{ inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(layoutId, root, false); view.setTag(AQuery.TAG_LAYOUT, layoutId);
AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = getApplicationContext(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello, this is a custom dialog!"); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create();
private void initView() { LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.ultimate_listview_layout, this); mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.ultimate_listview_swipe_layout); mBasicUltimateListView = (BasicUltimateListView) view.findViewById(R.id.basicUltimateListView); mSwipeRefreshLayout.setEnabled(false); mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); }
@Override public View newView(Context context, Cursor cursor, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); //noinspection ConstantConditions return inflater.inflate(R.layout.list_item, parent, false); }
isProgressColorSetBeforeDraw = false; isHeaderColorSetBeforeDraw = false; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.round_corner_with_icon_layout, this); setup(context, attrs); isProgressBarCreated = true; tv.setText("IconRoundCornerProgressBar"); addView(tv);
/** * Public constructor for a SuperToast. * * @param context A valid Context * @param style The desired Style */ public SuperToast(@NonNull Context context, @NonNull Style style) { this.mContext = context; this.mStyle = style; final LayoutInflater layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mView = onCreateView(context, layoutInflater, this.mStyle.type); this.mTextView = (TextView) this.mView.findViewById(R.id.message); }
private boolean getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isConnected(); }
@Test public void testNetworkStateVpn() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_VPN); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.NOT_ROAMING); }
/** * 判断wifi是否连接状态 * <p>需添加权限 android.permission.ACCESS_NETWORK_STATE</p> * * @param context 上下文 * @return true: 连接<br>false: 未连接 */ public static boolean isWifiConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); return cm != null && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI; }