From 1a417b3af269f62f2792164f356e1dd2758fda66 Mon Sep 17 00:00:00 2001 From: white-yj8109 <19310195525@163.com> Date: Sat, 24 Jan 2026 15:10:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E7=BC=BA=E9=99=B7=EF=BC=9A?= =?UTF-8?q?=E6=A1=8C=E9=9D=A2=E5=B0=8F=E7=BB=84=E4=BB=B6=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E3=80=81=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E9=97=AA=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/NoteEditActivity.java | 62 ++++++++++++++++++++++++----- src/ui/NotesPreferenceActivity.java | 3 ++ 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/ui/NoteEditActivity.java b/src/ui/NoteEditActivity.java index 2a09ae7..b9ac0ad 100644 --- a/src/ui/NoteEditActivity.java +++ b/src/ui/NoteEditActivity.java @@ -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, diff --git a/src/ui/NotesPreferenceActivity.java b/src/ui/NotesPreferenceActivity.java index f9569c8..341b589 100644 --- a/src/ui/NotesPreferenceActivity.java +++ b/src/ui/NotesPreferenceActivity.java @@ -91,6 +91,9 @@ public class NotesPreferenceActivity extends PreferenceActivity { mReceiver = new GTaskReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { + registerReceiver(mReceiver, filter, Context.RECEIVER_NOT_EXPORTED); + } else { registerReceiver(mReceiver, filter); mOriAccounts = null; -- 2.34.1