Java Code Examples for androidx.core.content.ContextCompat#getSystemService()

The following examples show how to use androidx.core.content.ContextCompat#getSystemService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckNetAspect.java    From AndroidProject with Apache License 2.0 6 votes vote down vote up
/**
 * 在连接点进行方法替换
 */
@Around("method() && @annotation(checkNet)")
public void aroundJoinPoint(ProceedingJoinPoint joinPoint, CheckNet checkNet) throws Throwable {
    Application application = ActivityStackManager.getInstance().getApplication();
    if (application != null) {
        ConnectivityManager manager = ContextCompat.getSystemService(application, ConnectivityManager.class);
        if (manager != null) {
            NetworkInfo info = manager.getActiveNetworkInfo();
            // 判断网络是否连接
            if (info == null || !info.isConnected()) {
                ToastUtils.show(R.string.common_network);
                return;
            }
        }
    }
    //执行原方法
    joinPoint.proceed();
}
 
Example 2
Source File: StatusAction.java    From AndroidProject with Apache License 2.0 6 votes vote down vote up
/**
 * 显示错误提示
 */
default void showError(View.OnClickListener listener) {
    Application application = ActivityStackManager.getInstance().getApplication();
    if (application != null) {
        ConnectivityManager manager = ContextCompat.getSystemService(application, ConnectivityManager.class);
        if (manager != null) {
            NetworkInfo info = manager.getActiveNetworkInfo();
            // 判断网络是否连接
            if (info == null || !info.isConnected()) {
                showLayout(R.drawable.ic_hint_nerwork, R.string.hint_layout_error_network, listener);
                return;
            }
        }
    }
    showLayout(R.drawable.ic_hint_error, R.string.hint_layout_error_request, listener);
}
 
Example 3
Source File: BlePermissionHelper.java    From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * check that the bluetooth is enabled
 * @return true if the bluetooth is enable false if we ask to the user to enable it
 */
private boolean enableBluetoothAdapter(){
    final BluetoothManager bluetoothManager =
            ContextCompat.getSystemService(mCtx,BluetoothManager.class);
    if(bluetoothManager==null)
        throw new IllegalStateException("Bluetooth adapter is needed by this app!");
    //the adapter is !=null since we request in the manifest to have the bt capability
    final BluetoothAdapter btAdapter = bluetoothManager.getAdapter();

    // Ensures Bluetooth is enabled on the device.  If Bluetooth is not currently enabled,
    // fire an intent to display a dialog asking the user to grant permission to enable it.
    if (!btAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        return false;
    }else
        return true;
}
 
Example 4
Source File: ContextUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ContextCompat.getSystemServiceを呼び出して取得した結果がnullならIllegalArgumentExceptionを投げる
 * (たぶんAPIレベルをチェックしていないときにnullになる)
 * @param context
 * @param serviceClass
 * @param <T>
 * @return
 * @throws IllegalArgumentException
 */
@NonNull
public static <T> T requireSystemService(@NonNull Context context, @NonNull Class<T> serviceClass)
	throws IllegalArgumentException {

	final T result = ContextCompat.getSystemService(context, serviceClass);
	if (result != null) {
		return result;
	} else {
		throw new IllegalArgumentException();
	}
}
 
Example 5
Source File: ConnectivityAwareCondition.java    From SnackEngage with MIT License 5 votes vote down vote up
@SuppressLint("MissingPermission")
protected void init(final Context context) {

    if (context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE) == PackageManager.PERMISSION_GRANTED) {
        connectivityManager = ContextCompat.getSystemService(context, ConnectivityManager.class);

        activeNetwork = connectivityManager.getActiveNetworkInfo();
    }
}
 
Example 6
Source File: Dispatcher.java    From picasso with Apache License 2.0 5 votes vote down vote up
@SuppressLint("MissingPermission")
void performRetry(BitmapHunter hunter) {
  if (hunter.isCancelled()) return;

  if (service.isShutdown()) {
    performError(hunter);
    return;
  }

  NetworkInfo networkInfo = null;
  if (scansNetworkChanges) {
    ConnectivityManager connectivityManager =
        ContextCompat.getSystemService(context, ConnectivityManager.class);
    if (connectivityManager != null) {
      networkInfo = connectivityManager.getActiveNetworkInfo();
    }
  }

  if (hunter.shouldRetry(airplaneMode, networkInfo)) {
    if (hunter.picasso.loggingEnabled) {
      log(OWNER_DISPATCHER, VERB_RETRYING, getLogIdsForHunter(hunter));
    }
    if (hunter.getException() instanceof NetworkRequestHandler.ContentLengthException) {
      hunter.data = hunter.data.newBuilder().networkPolicy(NetworkPolicy.NO_CACHE).build();
    }
    hunter.future = service.submit(hunter);
  } else {
    performError(hunter);
    // Mark for replay only if we observe network info changes and support replay.
    if (scansNetworkChanges && hunter.supportsReplay()) {
      markForReplay(hunter);
    }
  }
}
 
Example 7
Source File: Dispatcher.java    From picasso with Apache License 2.0 5 votes vote down vote up
@SuppressLint("MissingPermission")
@Override public void onReceive(Context context, Intent intent) {
  // On some versions of Android this may be called with a null Intent,
  // also without extras (getExtras() == null), in such case we use defaults.
  if (intent == null) {
    return;
  }
  final String action = intent.getAction();
  if (ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
    if (!intent.hasExtra(EXTRA_AIRPLANE_STATE)) {
      return; // No airplane state, ignore it. Should we query Utils.isAirplaneModeOn?
    }
    dispatcher.dispatchAirplaneModeChange(intent.getBooleanExtra(EXTRA_AIRPLANE_STATE, false));
  } else if (CONNECTIVITY_ACTION.equals(action)) {
    ConnectivityManager connectivityManager =
        ContextCompat.getSystemService(context, ConnectivityManager.class);
    NetworkInfo networkInfo;
    try {
      networkInfo = connectivityManager.getActiveNetworkInfo();
    } catch (RuntimeException re) {
      Log.w(TAG, "System UI crashed, ignoring attempt to change network state.");
      return;
    }
    if (networkInfo == null) {
      Log.w(TAG, "No default network is currently active, ignoring attempt to change "
          + "network state.");
      return;
    }
    dispatcher.dispatchNetworkStateChange(networkInfo);
  }
}
 
Example 8
Source File: Utils.java    From picasso with Apache License 2.0 5 votes vote down vote up
static int calculateMemoryCacheSize(Context context) {
  ActivityManager am = ContextCompat.getSystemService(context, ActivityManager.class);
  boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
  int memoryClass = largeHeap ? am.getLargeMemoryClass() : am.getMemoryClass();
  // Target ~15% of the available heap.
  return (int) (1024L * 1024L * memoryClass / 7);
}
 
Example 9
Source File: ServiceUtil.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static LocationManager getLocationManager(@NonNull Context context) {
  return ContextCompat.getSystemService(context, LocationManager.class);
}
 
Example 10
Source File: ContextAction.java    From AndroidProject with Apache License 2.0 4 votes vote down vote up
/**
 * 获取系统服务
 */
default <S> S getSystemService(@NonNull Class<S> serviceClass) {
    return ContextCompat.getSystemService(getContext(), serviceClass);
}
 
Example 11
Source File: ContextUtils.java    From libcommon with Apache License 2.0 2 votes vote down vote up
/**
 * ContextCompat.getSystemServiceを呼び出すだけ
 * (たぶんAPIレベルをチェックしていないときにnullになる)
 * @param context
 * @param serviceClass
 * @param <T>
 * @return
 */
@Nullable
public static <T> T getSystemService(@NonNull Context context, @NonNull Class<T> serviceClass) {
	return ContextCompat.getSystemService(context, serviceClass);
}