下面列出了怎么用androidx.annotation.AnyRes的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 创建桌面快捷方式
* @param className 快捷方式点击 Intent className(class.getName())
* @param name 快捷方式名称
* @param icon 快捷方式图标
* @return {@code true} success, {@code false} fail
*/
public static boolean addShortcut(final String className, final String name, @AnyRes final int icon) {
if (className != null && name != null) {
try {
// 快捷方式点击 Intent 跳转
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(DevUtils.getContext(), className);
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
return addShortcut(shortcutIntent, name, icon);
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "addShortcut");
}
}
return false;
}
/**
* 获取给定资源标识符的全名
* @param id resource identifier
* @return Integer
*/
public static String getResourceName(@AnyRes final int id) {
try {
return DevUtils.getContext().getResources().getResourceName(id);
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getResourceName");
}
return null;
}
@AnyRes
public static int getResId(Context context, String resName, String type) {
try {
return context.getClassLoader()
.loadClass(context.getPackageName() + ".R$" + type)
.getField(resName)
.getInt(null);
} catch (Exception e) {
return 0;
}
}
private static boolean resourceHasPackage(@AnyRes int resid) {
return (resid >>> 24) != 0;
}
/**
* 创建桌面快捷方式
* @param clazz 快捷方式点击 Intent class
* @param name 快捷方式名称
* @param icon 快捷方式图标
* @return {@code true} success, {@code false} fail
*/
public static boolean addShortcut(final Class clazz, final String name, @AnyRes final int icon) {
return (clazz != null) ? addShortcut(clazz.getName(), name, icon) : false;
}
/**
* 创建桌面快捷方式
* @param shortcutIntent 快捷方式点击 Intent 跳转
* @param name 快捷方式名称
* @param icon 快捷方式图标
* @return {@code true} success, {@code false} fail
*/
public static boolean addShortcut(final Intent shortcutIntent, final String name, @AnyRes final int icon) {
return addShortcut(shortcutIntent, name, icon, null);
}
/**
* A utility to return a URL reference to an image resource
*
* @param resourceId The id of the image resource
* @return The URL of the image resource
*/
public static String getResourceUrl(@AnyRes int resourceId) {
Resources resources = TvApp.getApplication().getApplicationContext().getResources();
return String.format("%s://%s/%s/%s", ContentResolver.SCHEME_ANDROID_RESOURCE, resources.getResourcePackageName(resourceId), resources.getResourceTypeName(resourceId), resources.getResourceEntryName(resourceId));
}