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;
}
}
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 "";
}
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;
}
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;
}
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);
}
Aggregations