下面列出了怎么用android.content.Intent.ShortcutIconResource的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns a bitmap suitable for the all apps view. If the package or the resource do not
* exist, it returns null.
*/
public static Bitmap createIconBitmap(ShortcutIconResource iconRes, Context context) {
PackageManager packageManager = context.getPackageManager();
// the resource
try {
Resources resources = packageManager.getResourcesForApplication(iconRes.packageName);
if (resources != null) {
final int id = resources.getIdentifier(iconRes.resourceName, null, null);
return createIconBitmap(resources.getDrawableForDensity(
id, LauncherAppState.getIDP(context).fillResIconDpi, context.getTheme()), context);
}
} catch (Exception e) {
// Icon not found.
}
return null;
}
/**
* Loads the icon from the cursor and updates the {@param info} if the icon is an app resource.
*/
private Bitmap loadIcon(ShortcutInfo info) {
Bitmap icon = null;
if (itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
String packageName = getString(iconPackageIndex);
String resourceName = getString(iconResourceIndex);
if (!TextUtils.isEmpty(packageName) || !TextUtils.isEmpty(resourceName)) {
info.iconResource = new ShortcutIconResource();
info.iconResource.packageName = packageName;
info.iconResource.resourceName = resourceName;
icon = LauncherIcons.createIconBitmap(info.iconResource, mContext);
}
}
if (icon == null) {
// Failed to load from resource, try loading from DB.
byte[] data = getBlob(iconIndex);
try {
icon = LauncherIcons.createIconBitmap(
BitmapFactory.decodeByteArray(data, 0, data.length), mContext);
} catch (Exception e) {
return null;
}
}
return icon;
}
public static void a(Context context, String str, String str2, int i) {
Uri parse = Uri.parse(str2);
if (parse == null) {
new StringBuilder(z[34]).append(str2);
z.b();
return;
}
Parcelable intent = new Intent(z[23], parse);
intent.setFlags(335544320);
Parcelable fromContext = ShortcutIconResource.fromContext(context, i);
Intent intent2 = new Intent(z[30]);
intent2.putExtra(z[33], false);
intent2.putExtra(z[35], str);
intent2.putExtra(z[32], intent);
intent2.putExtra(z[31], fromContext);
context.sendBroadcast(intent2);
}
/**
* 为程序创建桌面快捷方式
*
* @param activity Activity
*/
public static void addShortcut(Activity activity) {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
activity.getString(R.string.app_name));
shortcut.putExtra("duplicate", false); // 不允许重复创建
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(activity, activity.getClass().getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 快捷方式的图标
ShortcutIconResource iconRes = ShortcutIconResource.fromContext(
activity, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
activity.sendBroadcast(shortcut);
}
public Bitmap loadIcon(Cursor c, ShortcutInfo info, Context context) {
Bitmap icon = null;
int iconType = c.getInt(iconTypeIndex);
switch (iconType) {
case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
String packageName = c.getString(iconPackageIndex);
String resourceName = c.getString(iconResourceIndex);
if (!TextUtils.isEmpty(packageName) || !TextUtils.isEmpty(resourceName)) {
info.iconResource = new ShortcutIconResource();
info.iconResource.packageName = packageName;
info.iconResource.resourceName = resourceName;
icon = Utilities.createIconBitmap(packageName, resourceName, context);
}
if (icon == null) {
// Failed to load from resource, try loading from DB.
icon = Utilities.createIconBitmap(c, iconIndex, context);
}
break;
case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
icon = Utilities.createIconBitmap(c, iconIndex, context);
info.customIcon = icon != null;
break;
}
return icon;
}
/**
* 为程序创建桌面快捷方式
*
* @param activity Activity
* @param res res
*/
public static void addShortcut(Activity activity, int res) {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
activity.getString(R.string.app_name));
shortcut.putExtra("duplicate", false); // 不允许重复创建
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(activity, activity.getClass().getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
activity, res);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
activity.sendBroadcast(shortcut);
}
private void setUpShortCut() {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "baiduad");
shortcut.putExtra("duplicate", true);
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.putExtra("tName", "baiduad");
shortcutIntent.setClassName("com.baidu.adfolder",
"com.baidu.adfolder.FloatFolder");
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
this, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
Editor editor = sharedPreferences.edit();
editor.putBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, true);
editor.commit();
}
/**
* 为程序创建桌面快捷方式
*/
public void addShortcut() {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, ctx.getString(R.string.app_name));
// 不允许重复创建
shortcut.putExtra("duplicate", false);
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClass(ctx, WelcomeActivity.class);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(ctx, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
ctx.sendBroadcast(shortcut);
}
private void addShortcut() {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.Wifi_onekey));
shortcut.putExtra("duplicate", false); // 不允许重复创建
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, OneKeyWifi.class.getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
this, R.drawable.wifi_onekey_icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
private Intent createShortcutIntent(String activity, String shortcutName, ShortcutIconResource shortcutIcon, String action) {
Intent shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(this.getPackageName(), activity));
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcon);
intent.setAction(action);
return intent;
}
public static void createShortCut(Context context) {
if (context != null && PreferencesManager.getInstance().getShortcut()) {
PreferencesManager.getInstance().setShortcut(false);
if (!hasShortcut()) {
Intent shortcutintent = new Intent(ApkConstant.ACTION_INSTALL_SHORTCUT);
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra("android.intent.extra.shortcut.NAME", context.getString(2131099758));
shortcutintent.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", ShortcutIconResource.fromContext(context.getApplicationContext(), 2130838438));
shortcutintent.putExtra("android.intent.extra.shortcut.INTENT", new Intent(context.getApplicationContext(), SplashActivity.class));
context.sendBroadcast(shortcutintent);
}
}
}
private void quickLaunchProfile(String filename) {
// The meat of our shortcut
Intent shortcutIntent = new Intent (this, QuickLaunchActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.putExtra(U.NAME, filename);
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher);
// The result we are passing back from this activity
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
if(filename.equals("turn_off"))
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getStringArray(R.array.pref_notification_action_list)[0]);
else {
try {
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, U.getProfileTitle(this, filename));
} catch (IOException e) {
U.showToast(this, R.string.error_loading_list);
}
}
setResult(RESULT_OK, intent);
finish(); // Must call finish for result to be returned immediately
}
void createShortCut() {
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClass(this, OneKeyWifi.class);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.Wifi_onekey));
Parcelable shortIcon = Intent.ShortcutIconResource.fromContext(this,
R.drawable.wifi_onekey_icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortIcon);
setResult(RESULT_OK, intent);
}
/**
* Build and return list of items to be shown in dialog. Default
* implementation mixes activities matching {@link #mBaseIntent} from
* {@link #putIntentItems(Intent, List)} with any injected items from
* {@link Intent#EXTRA_SHORTCUT_NAME}. Override this method in subclasses to
* change the items shown.
*/
protected List<PickAdapter.Item> getItems() {
PackageManager packageManager = getPackageManager();
List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();
// Add any injected pick items
final Intent intent = getIntent();
ArrayList<String> labels =
intent.getStringArrayListExtra(Intent.EXTRA_SHORTCUT_NAME);
ArrayList<ShortcutIconResource> icons =
intent.getParcelableArrayListExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
if (labels != null && icons != null && labels.size() == icons.size()) {
for (int i = 0; i < labels.size(); i++) {
String label = labels.get(i);
Drawable icon = null;
try {
// Try loading icon from requested package
ShortcutIconResource iconResource = icons.get(i);
Resources res = packageManager.getResourcesForApplication(
iconResource.packageName);
icon = res.getDrawable(res.getIdentifier(
iconResource.resourceName, null, null));
} catch (NameNotFoundException e) {
// Ignore
}
items.add(new PickAdapter.Item(this, label, icon));
}
}
// Add any intent items if base was given
if (mBaseIntent != null) {
putIntentItems(mBaseIntent, items);
}
if ( items.size() == 0 ){
return null;
}
return items;
}
private void addShortcut(String activity, int nameId, int iconId) {
Intent intent = createShortcutIntent(activity, getString(nameId), Intent.ShortcutIconResource.fromContext(this, iconId),
"com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
}
@Override
@SuppressWarnings("deprecation")
public void onReceive(Context context, Intent intent) {
Intent shortcutIntent = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
String uri = shortcutIntent.toUri(0);
if (DatabaseHelper.hasShortcut(context, uri) || isAppLink(uri)) {
return;
}
if (shortcutIntent.getAction() == null) {
shortcutIntent.setAction(Intent.ACTION_VIEW);
}
String name = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Bitmap icon = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
String packageName = null;
String resourceName = null;
ContentValues values = new ContentValues();
if (icon == null) {
ShortcutIconResource resource = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
if (resource != null) {
packageName = resource.packageName;
resourceName = resource.resourceName;
PackageManager pm = context.getPackageManager();
try {
Resources res = pm.getResourcesForApplication(packageName);
int id = res.getIdentifier(resourceName, "png", packageName);
Drawable d = res.getDrawable(id);
icon = ((BitmapDrawable) d).getBitmap();
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
} else {
//invalid shortcut
return;
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
icon.compress(CompressFormat.PNG, 100, out);
values.put("icon", out.toString());
saveIcon(context, uri, icon);
values.put("name", name);
values.put("uri", uri);
values.put("package", packageName);
values.put("resource", resourceName);
values.put("categories", "");
DatabaseHelper.insertShortcut(context, values);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_notif_clear);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_app_launcher);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_screenshot);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.ic_lockscreen_unlock);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_home);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_kill_app);
}
@Override
protected ShortcutIconResource getIconResource() {
return null;
}
public ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, mIconResId);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_google_now);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_volume_panel);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_torch);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_screenrecord);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_recent_apps);
}
@Override
protected ShortcutIconResource getIconResource() {
return ShortcutIconResource.fromContext(mContext, R.drawable.shortcut_launcher_drawer);
}