Android上的桌面快捷方式大致分为2个版本,在低于API 26的系统上,通过以下方式:
private void addShortcutForLowOS() {
String INTENT_ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
Intent shortcut = new Intent(INTENT_ACTION_INSTALL_SHORTCUT);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appeltTitle);
Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
launcherIntent.setClassName(this, "ShortCutActivity");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
launcherIntent.putExtra(APPELT_TITLE_KEY, appeltTitle);
launcherIntent.putExtra(APPELT_FUNCTION_ID, functionId);
Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, resId);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
在高于API 26的系统上,通过以下方式:
private void addShortcutForHighOS() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
if (shortcutManager.isRequestPinShortcutSupported()) {
List<ShortcutInfo> shortcutInfoList = shortcutManager.getPinnedShortcuts();
if (shortcutInfoList != null && !shortcutInfoList.isEmpty()) {
for (ShortcutInfo shortcutInfo : shortcutInfoList) {
Intent mIntent = shortcutInfo.getIntent();
if (mIntent == null) {
continue;
}
String mFunctionId = mIntent.getStringExtra(APPELT_FUNCTION_ID);
if (!TextUtils.isEmpty(mFunctionId) && mFunctionId.equals(functionId)) {
// 已添加过
return;
}
}
}
Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
launcherIntent.setClassName(this, "ShortCutActivity");
launcherIntent.setAction(Intent.ACTION_VIEW);
launcherIntent.putExtra(APPELT_TITLE_KEY, appeltTitle);
launcherIntent.putExtra(APPELT_FUNCTION_ID, functionId);
ShortcutInfo info = new ShortcutInfo.Builder(this, appeltTitle)
.setIcon(Icon.createWithBitmap(bitmap))
.setShortLabel(appeltTitle)
.setIntent(launcherIntent)
.build();
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(
this,
0,
new Intent(),
PendingIntent.FLAG_UPDATE_CURRENT);
shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
} else {
// 不支持
}
}
}
在ShortCutActivity类中,实现功能如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 获取参数
String appeltTitle = getIntent().getStringExtra(APPELT_TITLE_KEY);
String functionId = getIntent().getStringExtra(APPELT_FUNCTION_ID);
PackageManager packageManager = getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(getPackageName());
intent.setClass(this, StartAppSplash.class);
intent.putExtra(APPELT_TITLE_KEY, appeltTitle);
intent.putExtra(APPELT_FUNCTION_ID, functionId);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
try {
pendingIntent.send();
} catch (Exception e) {
e.printStackTrace();
}
finish();
}
在AndroidManif.xml中,对ShortCutActivity类的声明如下:
<activity
android:name="ShortCutActivity"
// launchMode的模式,与具体的业务有关
android:launchMode="singleTask"
android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>