Android Shortcuts 爲其他應用創建桌面快捷方式,替換圖標等
shortcuts功能自android N_MR1(7.1,api 25)引入,使用ShortcutManager管理,
關於創建桌面圖標pinned shortcuts是android O(8.0 api 26添加的,但是可以通過support包在低版本兼容)
- 在查看官方文檔時發現有這麼一段話
Note: See also the support library APIs, isRequestPinShortcutSupported() and requestPinShortcut(), which work on Android 7.1 (API level 25) and lower. The support library falls back to the deprecated EXTRA_SHORTCUT_INTENT extra to attempt the pinning process.
- 創建桌面快捷圖標在安卓7.1之前可以通過support(android.support.v4.content.pm)包兼容
創建
直接看代碼
class ShortcutIconBean{
@JsonName("name")
private String name;
@JsonName("packageName")
private String packageName;
@JsonName("iconPath")
private String iconPath;
}
class xxx{
/**添加快捷方式
*@param shortcutInfoIntent 創建桌面圖標以後點擊的執行邏輯
*
* */
private void addShortcut(Intent shortcutInfoIntent,ShortcutIconBean shortCut) {
Bitmap bitmap = BitmapUtils.decodeFile(shortCut.getIconPath());
if (bitmap == null){
Log.i(TAG,"bitmap == null");
return;
}
String id = mThemeId + "-" + shortCut.getName() + "-" + System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.i(TAG," Android O");
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo info = new ShortcutInfo.Builder(this, id)
.setIcon(Icon.createWithBitmap(bitmap))
.setShortLabel(shortCut.getName())
.setIntent(shortcutInfoIntent)
.build();
Intent successCallback = new Intent(ACTION);
/* //In your app's manifest file, add ACTION_CREATE_SHORTCUT to the activity's <intent-filter> element.
// 使用這個方式獲取intent需要自己創建一個ACTIVITY管理創建成功回調,在ACTIVITY聲明中加 ACTION_CREATE_SHORTCUT 過濾
Intent successCallback =
shortcutManager.createShortcutResultIntent(info);*/
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(this, 200,
successCallback, PendingIntent.FLAG_UPDATE_CURRENT);
shortcutManager.requestPinShortcut( info, shortcutCallbackIntent.getIntentSender());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(this, id)
.setIcon(IconCompat.createWithBitmap(bitmap))
.setShortLabel(shortCut.getName())
.setIntent(shortcutInfoIntent)
.build();
Intent successCallback = new Intent(ACTION);
//ACTION 處理創建結果的廣播
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(this, 200,
successCallback, PendingIntent.FLAG_UPDATE_CURRENT);
ShortcutManagerCompat.requestPinShortcut(this, info, shortcutCallbackIntent.getIntentSender());
// 註釋部分爲發廣播的實現方式,不在使用
/* Log.i(TAG," Android N");
Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
launcherIntent.setClass( this, JumpActivity.class);
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launcherIntent.putExtra("packageName",shortCut.getPackageName());
// Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
Intent installer = new Intent();
//false標示不重複創建
installer.putExtra("duplicate", false);
installer.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
//設置應用的名稱
installer.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCut.getName());
//設置圖標
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 128, 128, true);
installer.putExtra(Intent.EXTRA_SHORTCUT_ICON,bitmap);
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//發送安裝桌面圖標的通知
sendBroadcast(installer);
Toast.makeText(this,"製作完成,請到桌面查看",Toast.LENGTH_SHORT).show();*/
}
}
}
版本判斷然後執行不同代碼,其實不同之處僅僅是將 ShortcutInfo 使用 ShortcutInfoCompat進行,
關於動態添加刪除以及長按圖標出現二級網上說得比較多
可參考shortcuts