|
|
|
|
@ -1127,15 +1127,42 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
Intent shortcutIntent = new Intent(this, NoteEditActivity.class);
|
|
|
|
|
shortcutIntent.setAction(Intent.ACTION_VIEW);//设置意图操作行为为查看
|
|
|
|
|
shortcutIntent.putExtra(Intent.EXTRA_UID, mWorkingNote.getNoteId());
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);//将意图添加到发送者意图中
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_NAME,
|
|
|
|
|
makeShortcutIconTitle(mWorkingNote.getContent()));//将快捷图标标题添加到发送者意图中
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
|
|
|
|
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon_app));
|
|
|
|
|
sender.putExtra("duplicate", true);//将重复项添加到发送者意图中
|
|
|
|
|
sender.setAction("com.android.launcher.action.INSTALL_SHORTCUT");//设置意图操作行为为安装快捷图标
|
|
|
|
|
showToast(R.string.info_note_enter_desktop);//显示提示消息(成功)
|
|
|
|
|
sendBroadcast(sender);
|
|
|
|
|
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
|
|
|
|
|
|
// 获取快捷方式名称
|
|
|
|
|
String shortcutName = makeShortcutIconTitle(mWorkingNote.getContent());
|
|
|
|
|
|
|
|
|
|
// 检查Android版本,使用相应的API
|
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
|
|
|
// Android 8.0+ 使用ShortcutManager API
|
|
|
|
|
android.content.pm.ShortcutManager shortcutManager = getSystemService(android.content.pm.ShortcutManager.class);
|
|
|
|
|
if (shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()) {
|
|
|
|
|
// 创建ShortcutInfo对象
|
|
|
|
|
android.content.pm.ShortcutInfo.Builder builder = new android.content.pm.ShortcutInfo.Builder(this, "note_" + mWorkingNote.getNoteId());
|
|
|
|
|
builder.setShortLabel(shortcutName);
|
|
|
|
|
builder.setLongLabel(shortcutName);
|
|
|
|
|
builder.setIntent(shortcutIntent);
|
|
|
|
|
// 设置图标
|
|
|
|
|
builder.setIcon(android.graphics.drawable.Icon.createWithResource(this, R.drawable.icon_app));
|
|
|
|
|
|
|
|
|
|
// 创建PendingIntent用于确认
|
|
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
|
|
intent.setAction("android.intent.action.CREATE_SHORTCUT");
|
|
|
|
|
intent.putExtra("note_id", mWorkingNote.getNoteId());
|
|
|
|
|
android.app.PendingIntent pendingIntent = android.app.PendingIntent.getActivity(this, 0, intent, 0);
|
|
|
|
|
|
|
|
|
|
// 请求创建快捷方式
|
|
|
|
|
shortcutManager.requestPinShortcut(builder.build(), pendingIntent.getIntentSender());
|
|
|
|
|
showToast(R.string.info_note_enter_desktop);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果ShortcutManager不可用,使用旧方式
|
|
|
|
|
sendShortcutBroadcast(shortcutIntent, shortcutName);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Android 7.1及以下使用旧的广播方式
|
|
|
|
|
sendShortcutBroadcast(shortcutIntent, shortcutName);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/**
|
|
|
|
|
* There is the condition that user has input nothing (the note is
|
|
|
|
|
@ -1147,7 +1174,22 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String makeShortcutIconTitle(String content) {//创建快捷图标标题
|
|
|
|
|
/**
|
|
|
|
|
* 使用旧的广播方式创建快捷方式(兼容Android 7.1及以下)
|
|
|
|
|
*/
|
|
|
|
|
private void sendShortcutBroadcast(Intent shortcutIntent, String shortcutName) {
|
|
|
|
|
Intent sender = new Intent();
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
|
|
|
|
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon_app));
|
|
|
|
|
sender.putExtra("duplicate", true);
|
|
|
|
|
sender.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
|
|
|
|
|
showToast(R.string.info_note_enter_desktop);
|
|
|
|
|
sendBroadcast(sender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String makeShortcutIconTitle(String content) {
|
|
|
|
|
content = content.replace(TAG_CHECKED, "");
|
|
|
|
|
content = content.replace(TAG_UNCHECKED, "");
|
|
|
|
|
return content.length() > SHORTCUT_ICON_TITLE_MAX_LEN ? content.substring(0,
|
|
|
|
|
|