Search in sources :

Example 11 with RequiresPermission

use of androidx.annotation.RequiresPermission in project AndroidUtilCode by Blankj.

the class NetworkUtils method isAvailableByDns.

/**
 * Return whether network is available using domain.
 * <p>Must hold {@code <uses-permission android:name="android.permission.INTERNET" />}</p>
 *
 * @param domain The name of domain.
 * @return {@code true}: yes<br>{@code false}: no
 */
@RequiresPermission(INTERNET)
public static boolean isAvailableByDns(final String domain) {
    final String realDomain = TextUtils.isEmpty(domain) ? "www.baidu.com" : domain;
    InetAddress inetAddress;
    try {
        inetAddress = InetAddress.getByName(realDomain);
        return inetAddress != null;
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return false;
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetAddress(java.net.InetAddress) RequiresPermission(androidx.annotation.RequiresPermission)

Example 12 with RequiresPermission

use of androidx.annotation.RequiresPermission in project AndroidUtilCode by Blankj.

the class PhoneUtils method getDeviceId.

/**
 * Return the unique device id.
 * <p>If the version of SDK is greater than 28, it will return an empty string.</p>
 * <p>Must hold {@code <uses-permission android:name="android.permission.READ_PHONE_STATE" />}</p>
 *
 * @return the unique device id
 */
@SuppressLint("HardwareIds")
@RequiresPermission(READ_PHONE_STATE)
public static String getDeviceId() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        return "";
    }
    TelephonyManager tm = getTelephonyManager();
    String deviceId = tm.getDeviceId();
    if (!TextUtils.isEmpty(deviceId))
        return deviceId;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String imei = tm.getImei();
        if (!TextUtils.isEmpty(imei))
            return imei;
        String meid = tm.getMeid();
        return TextUtils.isEmpty(meid) ? "" : meid;
    }
    return "";
}
Also used : TelephonyManager(android.telephony.TelephonyManager) SuppressLint(android.annotation.SuppressLint) RequiresPermission(androidx.annotation.RequiresPermission)

Example 13 with RequiresPermission

use of androidx.annotation.RequiresPermission in project AndroidUtilCode by Blankj.

the class ProcessUtils method getAllBackgroundProcesses.

/**
 * Return all background processes.
 * <p>Must hold {@code <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />}</p>
 *
 * @return all background processes
 */
@RequiresPermission(KILL_BACKGROUND_PROCESSES)
public static Set<String> getAllBackgroundProcesses() {
    ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
    Set<String> set = new HashSet<>();
    if (info != null) {
        for (ActivityManager.RunningAppProcessInfo aInfo : info) {
            Collections.addAll(set, aInfo.pkgList);
        }
    }
    return set;
}
Also used : ActivityManager(android.app.ActivityManager) HashSet(java.util.HashSet) RequiresPermission(androidx.annotation.RequiresPermission)

Example 14 with RequiresPermission

use of androidx.annotation.RequiresPermission in project AndroidUtilCode by Blankj.

the class ProcessUtils method killBackgroundProcesses.

/**
 * Kill background processes.
 * <p>Must hold {@code <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />}</p>
 *
 * @param packageName The name of the package.
 * @return {@code true}: success<br>{@code false}: fail
 */
@RequiresPermission(KILL_BACKGROUND_PROCESSES)
public static boolean killBackgroundProcesses(@NonNull final String packageName) {
    ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
    if (info == null || info.size() == 0)
        return true;
    for (ActivityManager.RunningAppProcessInfo aInfo : info) {
        if (Arrays.asList(aInfo.pkgList).contains(packageName)) {
            am.killBackgroundProcesses(packageName);
        }
    }
    info = am.getRunningAppProcesses();
    if (info == null || info.size() == 0)
        return true;
    for (ActivityManager.RunningAppProcessInfo aInfo : info) {
        if (Arrays.asList(aInfo.pkgList).contains(packageName)) {
            return false;
        }
    }
    return true;
}
Also used : ActivityManager(android.app.ActivityManager) RequiresPermission(androidx.annotation.RequiresPermission)

Example 15 with RequiresPermission

use of androidx.annotation.RequiresPermission in project AndroidUtilCode by Blankj.

the class VibrateUtils method vibrate.

/**
 * Vibrate.
 * <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
 *
 * @param milliseconds The number of milliseconds to vibrate.
 */
@RequiresPermission(VIBRATE)
public static void vibrate(final long milliseconds) {
    Vibrator vibrator = getVibrator();
    if (vibrator == null)
        return;
    vibrator.vibrate(milliseconds);
}
Also used : Vibrator(android.os.Vibrator) RequiresPermission(androidx.annotation.RequiresPermission)

Aggregations

RequiresPermission (androidx.annotation.RequiresPermission)43 SuppressLint (android.annotation.SuppressLint)11 WifiManager (android.net.wifi.WifiManager)5 TelephonyManager (android.telephony.TelephonyManager)4 ActivityManager (android.app.ActivityManager)3 Intent (android.content.Intent)3 SurfaceTexture (android.graphics.SurfaceTexture)3 Vibrator (android.os.Vibrator)3 Size (com.google.android.gms.common.images.Size)3 TargetApi (android.annotation.TargetApi)2 ConnectivityManager (android.net.ConnectivityManager)2 NetworkInfo (android.net.NetworkInfo)2 Bundle (android.os.Bundle)2 PowerManager (android.os.PowerManager)2 SpannableString (android.text.SpannableString)2 Rational (android.util.Rational)2 Size (android.util.Size)2 SurfaceView (android.view.SurfaceView)2 CameraSelector (androidx.camera.core.CameraSelector)2 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)2