如何使用 DELPHI 在 Android 设备上更改通知中的小图标图像
How to change small icon image in notification on Android device with DELPHI
有什么方法可以更改 Android 应用程序通知中的小图标图像(默认使用的应用程序图标)Delphi?
轻松创建通知的标准方法是:
var
aNotification: TNotification;
begin
aNotification:= aNotificationCenter.CreateNotification;
try
aNotification.Name:= 'Notification Name';
aNotification.Title:= 'Notification Title';
aNotification.AlertBody:= 'Notification Alert Body';
aNotification.EnableSound:= False;
aWorkNotification.Number:= 1;
aNotificationCenter.PresentNotification(aNotification);
finally
aNotification.Free;
end;
end;
我知道 Java 的解决方案,但我不知道如何在 Delphi 中编写相同的程序。
有人告诉我吗?
首先,准备png格式的图标(16 x 16,命名如ic_test)。使用 Deployment 将 png 图标保存到这些目录:
res\drawable-ldpi\
res\drawable-mdpi\
res\drawable-hdpi\
res\drawable-xhdpi\
res\drawable-xxhdpi\
然后使用下面的代码:
.
.
var
Ntf: JNotification;
ntfBuilder: JNotificationCompat_Builder;
ntfManager: JNotificationManager;
.
.
ntfBuilder:= TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);
ntfBuilder.setSmallIcon(TAndroidHelper.Context.getResources.getIdentifier(StringToJString('ic_test'), StringToJString('drawable'), TAndroidHelper.Context.getPackageName));
ntfBuilder.setContentTitle(StrToJCharSequence('Content Title'));
ntfBuilder.setContentText(StrToJCharSequence('Content Test'));
ntfBuilder.setAutoCancel(True);
Ntf:= ntfBuilder.build;
ntfManager:= TJNotificationManager.Wrap((TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE) as ILocalObject).GetObjectID);
ntfManager.notify(1, Ntf);
.
.
如果你想使用内置系统图标使用这个常量
const
r_drawable_alert_dark_frame = 17301504;
r_drawable_alert_light_frame = 17301505;
r_drawable_arrow_down_float = 17301506;
r_drawable_arrow_up_float = 17301507;
r_drawable_bottom_bar = 17301658;
r_drawable_btn_default = 17301508;
r_drawable_btn_default_small = 17301509;
r_drawable_btn_dialog = 17301527;
r_drawable_btn_dropdown = 17301510;
r_drawable_btn_minus = 17301511;
r_drawable_btn_plus = 17301512;
r_drawable_btn_radio = 17301513;
r_drawable_btn_star = 17301514;
r_drawable_btn_star_big_off = 17301515;
r_drawable_btn_star_big_on = 17301516;
r_drawable_button_onoff_indicator_off = 17301518;
r_drawable_button_onoff_indicator_on = 17301517;
r_drawable_checkbox_off_background = 17301519;
r_drawable_checkbox_on_background = 17301520;
r_drawable_dark_header = 17301669;
r_drawable_dialog_frame = 17301521;
r_drawable_dialog_holo_dark_frame = 17301682;
r_drawable_dialog_holo_light_frame = 17301683;
r_drawable_divider_horizontal_bright = 17301522;
r_drawable_divider_horizontal_dark = 17301524;
r_drawable_divider_horizontal_dim_dark = 17301525;
r_drawable_divider_horizontal_textfield = 17301523;
r_drawable_edit_text = 17301526;
r_drawable_editbox_background = 17301528;
r_drawable_editbox_background_normal = 17301529;
r_drawable_editbox_dropdown_dark_frame = 17301530;
r_drawable_editbox_dropdown_light_frame = 17301531;
r_drawable_gallery_thumb = 17301532;
r_drawable_ic_btn_speak_now = 17301668;
r_drawable_ic_delete = 17301533;
r_drawable_ic_dialog_alert = 17301543;
r_drawable_ic_dialog_dialer = 17301544;
r_drawable_ic_dialog_email = 17301545;
r_drawable_ic_dialog_info = 17301659;
r_drawable_ic_dialog_map = 17301546;
r_drawable_ic_input_add = 17301547;
r_drawable_ic_input_delete = 17301548;
r_drawable_ic_input_get = 17301549;
r_drawable_ic_lock_idle_alarm = 17301550;
r_drawable_ic_lock_idle_charging = 17301534;
r_drawable_ic_lock_idle_lock = 17301535;
r_drawable_ic_lock_idle_low_battery = 17301536;
r_drawable_ic_lock_lock = 17301551;
r_drawable_ic_lock_power_off = 17301552;
r_drawable_ic_lock_silent_mode = 17301553;
r_drawable_ic_lock_silent_mode_off = 17301554;
r_drawable_ic_media_ff = 17301537;
r_drawable_ic_media_next = 17301538;
r_drawable_ic_media_pause = 17301539;
r_drawable_ic_media_play = 17301540;
r_drawable_ic_media_previous = 17301541;
r_drawable_ic_media_rew = 17301542;
r_drawable_ic_menu_add = 17301555;
r_drawable_ic_menu_agenda = 17301556;
r_drawable_ic_menu_always_landscape_portrait = 17301557;
r_drawable_ic_menu_call = 17301558;
r_drawable_ic_menu_camera = 17301559;
r_drawable_ic_menu_close_clear_cancel = 17301560;
r_drawable_ic_menu_compass = 17301561;
r_drawable_ic_menu_crop = 17301562;
r_drawable_ic_menu_day = 17301563;
r_drawable_ic_menu_delete = 17301564;
r_drawable_ic_menu_directions = 17301565;
r_drawable_ic_menu_edit = 17301566;
r_drawable_ic_menu_gallery = 17301567;
r_drawable_ic_menu_help = 17301568;
r_drawable_ic_menu_info_details = 17301569;
r_drawable_ic_menu_manage = 17301570;
r_drawable_ic_menu_mapmode = 17301571;
r_drawable_ic_menu_month = 17301572;
r_drawable_ic_menu_more = 17301573;
r_drawable_ic_menu_my_calendar = 17301574;
r_drawable_ic_menu_mylocation = 17301575;
r_drawable_ic_menu_myplaces = 17301576;
r_drawable_ic_menu_preferences = 17301577;
r_drawable_ic_menu_recent_history = 17301578;
r_drawable_ic_menu_report_image = 17301579;
r_drawable_ic_menu_revert = 17301580;
r_drawable_ic_menu_rotate = 17301581;
r_drawable_ic_menu_save = 17301582;
r_drawable_ic_menu_search = 17301583;
r_drawable_ic_menu_send = 17301584;
r_drawable_ic_menu_set_as = 17301585;
r_drawable_ic_menu_share = 17301586;
r_drawable_ic_menu_slideshow = 17301587;
r_drawable_ic_menu_sort_alphabetically = 17301660;
r_drawable_ic_menu_sort_by_size = 17301661;
r_drawable_ic_menu_today = 17301588;
r_drawable_ic_menu_upload = 17301589;
r_drawable_ic_menu_upload_you_tube = 17301590;
r_drawable_ic_menu_view = 17301591;
r_drawable_ic_menu_week = 17301592;
r_drawable_ic_menu_zoom = 17301593;
r_drawable_ic_notification_clear_all = 17301594;
r_drawable_ic_notification_overlay = 17301595;
r_drawable_ic_partial_secure = 17301596;
r_drawable_ic_popup_disk_full = 17301597;
r_drawable_ic_popup_reminder = 17301598;
r_drawable_ic_popup_sync = 17301599;
r_drawable_ic_search_category_default = 17301600;
r_drawable_ic_secure = 17301601;
r_drawable_list_selector_background = 17301602;
r_drawable_menu_frame = 17301603;
r_drawable_menu_full_frame = 17301604;
r_drawable_menuitem_background = 17301605;
r_drawable_picture_frame = 17301606;
r_drawable_presence_audio_away = 17301679;
r_drawable_presence_audio_busy = 17301680;
r_drawable_presence_audio_online = 17301681;
r_drawable_presence_away = 17301607;
r_drawable_presence_busy = 17301608;
r_drawable_presence_invisible = 17301609;
r_drawable_presence_offline = 17301610;
r_drawable_presence_online = 17301611;
r_drawable_presence_video_away = 17301676;
r_drawable_presence_video_busy = 17301677;
r_drawable_presence_video_online = 17301678;
r_drawable_progress_horizontal = 17301612;
r_drawable_progress_indeterminate_horizontal = 17301613;
r_drawable_radiobutton_off_background = 17301614;
r_drawable_radiobutton_on_background = 17301615;
r_drawable_screen_background_dark = 17301656;
r_drawable_screen_background_dark_transparent = 17301673;
r_drawable_screen_background_light = 17301657;
r_drawable_screen_background_light_transparent = 17301674;
r_drawable_spinner_background = 17301616;
r_drawable_spinner_dropdown_background = 17301617;
r_drawable_star_big_off = 17301619;
r_drawable_star_big_on = 17301618;
r_drawable_star_off = 17301621;
r_drawable_star_on = 17301620;
r_drawable_stat_notify_call_mute = 17301622;
r_drawable_stat_notify_chat = 17301623;
r_drawable_stat_notify_error = 17301624;
r_drawable_stat_notify_missed_call = 17301631;
r_drawable_stat_notify_more = 17301625;
r_drawable_stat_notify_sdcard = 17301626;
r_drawable_stat_notify_sdcard_prepare = 17301675;
r_drawable_stat_notify_sdcard_usb = 17301627;
r_drawable_stat_notify_sync = 17301628;
r_drawable_stat_notify_sync_noanim = 17301629;
r_drawable_stat_notify_voicemail = 17301630;
r_drawable_stat_sys_data_bluetooth = 17301632;
r_drawable_stat_sys_download = 17301633;
r_drawable_stat_sys_download_done = 17301634;
r_drawable_stat_sys_headset = 17301635;
r_drawable_stat_sys_phone_call = 17301636;
r_drawable_stat_sys_phone_call_forward = 17301637;
r_drawable_stat_sys_phone_call_on_hold = 17301638;
r_drawable_stat_sys_speakerphone = 17301639;
r_drawable_stat_sys_upload = 17301640;
r_drawable_stat_sys_upload_done = 17301641;
r_drawable_stat_sys_vp_phone_call = 17301671;
r_drawable_stat_sys_vp_phone_call_on_hold = 17301672;
r_drawable_stat_sys_warning = 17301642;
r_drawable_status_bar_item_app_background = 17301643;
r_drawable_status_bar_item_background = 17301644;
r_drawable_sym_action_call = 17301645;
r_drawable_sym_action_chat = 17301646;
r_drawable_sym_action_email = 17301647;
r_drawable_sym_call_incoming = 17301648;
r_drawable_sym_call_missed = 17301649;
r_drawable_sym_call_outgoing = 17301650;
r_drawable_sym_contact_card = 17301652;
r_drawable_sym_def_app_icon = 17301651;
r_drawable_title_bar = 17301653;
r_drawable_title_bar_tall = 17301670;
r_drawable_toast_frame = 17301654;
r_drawable_zoom_plate = 17301655;
使用以下代码(例如,对于 android 相当于 R.drawable.ic _menu_send):
ntfBuilder.setSmallIcon(r_drawable_ic_menu_send);
因为它在 Android 11 和 Delphi 10.3 上对我不起作用,
我在 System.Notification 和 System.Android.Notification 文件中进行了更改;
(我将它们复制到我的路径并将它们添加到我的应用程序中所以原始文件没问题)
System.Notification 变化:
我向 TNotification 添加了一个新的 public 字段:
SmallIconID : Integer;
并且在构造函数中我用-1初始化;
SmallIconId := -1;
System.Android.Notification 变化:
函数中TNotificationCenterAndroid.CreateNativeNotification
我在 if TOSVersion.Check(5) 行之前添加了这段代码:
if ANotification.SmallIconID > -1 then
Builder := Builder.setSmallIcon(ANotification.SmallIconID);
现在我可以在我的应用程序中设置小图标,它也在 Android 11 上运行。
有什么方法可以更改 Android 应用程序通知中的小图标图像(默认使用的应用程序图标)Delphi?
轻松创建通知的标准方法是:
var
aNotification: TNotification;
begin
aNotification:= aNotificationCenter.CreateNotification;
try
aNotification.Name:= 'Notification Name';
aNotification.Title:= 'Notification Title';
aNotification.AlertBody:= 'Notification Alert Body';
aNotification.EnableSound:= False;
aWorkNotification.Number:= 1;
aNotificationCenter.PresentNotification(aNotification);
finally
aNotification.Free;
end;
end;
我知道 Java 的解决方案,但我不知道如何在 Delphi 中编写相同的程序。
有人告诉我吗?
首先,准备png格式的图标(16 x 16,命名如ic_test)。使用 Deployment 将 png 图标保存到这些目录:
res\drawable-ldpi\
res\drawable-mdpi\
res\drawable-hdpi\
res\drawable-xhdpi\
res\drawable-xxhdpi\
然后使用下面的代码:
.
.
var
Ntf: JNotification;
ntfBuilder: JNotificationCompat_Builder;
ntfManager: JNotificationManager;
.
.
ntfBuilder:= TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);
ntfBuilder.setSmallIcon(TAndroidHelper.Context.getResources.getIdentifier(StringToJString('ic_test'), StringToJString('drawable'), TAndroidHelper.Context.getPackageName));
ntfBuilder.setContentTitle(StrToJCharSequence('Content Title'));
ntfBuilder.setContentText(StrToJCharSequence('Content Test'));
ntfBuilder.setAutoCancel(True);
Ntf:= ntfBuilder.build;
ntfManager:= TJNotificationManager.Wrap((TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE) as ILocalObject).GetObjectID);
ntfManager.notify(1, Ntf);
.
.
如果你想使用内置系统图标使用这个常量
const
r_drawable_alert_dark_frame = 17301504;
r_drawable_alert_light_frame = 17301505;
r_drawable_arrow_down_float = 17301506;
r_drawable_arrow_up_float = 17301507;
r_drawable_bottom_bar = 17301658;
r_drawable_btn_default = 17301508;
r_drawable_btn_default_small = 17301509;
r_drawable_btn_dialog = 17301527;
r_drawable_btn_dropdown = 17301510;
r_drawable_btn_minus = 17301511;
r_drawable_btn_plus = 17301512;
r_drawable_btn_radio = 17301513;
r_drawable_btn_star = 17301514;
r_drawable_btn_star_big_off = 17301515;
r_drawable_btn_star_big_on = 17301516;
r_drawable_button_onoff_indicator_off = 17301518;
r_drawable_button_onoff_indicator_on = 17301517;
r_drawable_checkbox_off_background = 17301519;
r_drawable_checkbox_on_background = 17301520;
r_drawable_dark_header = 17301669;
r_drawable_dialog_frame = 17301521;
r_drawable_dialog_holo_dark_frame = 17301682;
r_drawable_dialog_holo_light_frame = 17301683;
r_drawable_divider_horizontal_bright = 17301522;
r_drawable_divider_horizontal_dark = 17301524;
r_drawable_divider_horizontal_dim_dark = 17301525;
r_drawable_divider_horizontal_textfield = 17301523;
r_drawable_edit_text = 17301526;
r_drawable_editbox_background = 17301528;
r_drawable_editbox_background_normal = 17301529;
r_drawable_editbox_dropdown_dark_frame = 17301530;
r_drawable_editbox_dropdown_light_frame = 17301531;
r_drawable_gallery_thumb = 17301532;
r_drawable_ic_btn_speak_now = 17301668;
r_drawable_ic_delete = 17301533;
r_drawable_ic_dialog_alert = 17301543;
r_drawable_ic_dialog_dialer = 17301544;
r_drawable_ic_dialog_email = 17301545;
r_drawable_ic_dialog_info = 17301659;
r_drawable_ic_dialog_map = 17301546;
r_drawable_ic_input_add = 17301547;
r_drawable_ic_input_delete = 17301548;
r_drawable_ic_input_get = 17301549;
r_drawable_ic_lock_idle_alarm = 17301550;
r_drawable_ic_lock_idle_charging = 17301534;
r_drawable_ic_lock_idle_lock = 17301535;
r_drawable_ic_lock_idle_low_battery = 17301536;
r_drawable_ic_lock_lock = 17301551;
r_drawable_ic_lock_power_off = 17301552;
r_drawable_ic_lock_silent_mode = 17301553;
r_drawable_ic_lock_silent_mode_off = 17301554;
r_drawable_ic_media_ff = 17301537;
r_drawable_ic_media_next = 17301538;
r_drawable_ic_media_pause = 17301539;
r_drawable_ic_media_play = 17301540;
r_drawable_ic_media_previous = 17301541;
r_drawable_ic_media_rew = 17301542;
r_drawable_ic_menu_add = 17301555;
r_drawable_ic_menu_agenda = 17301556;
r_drawable_ic_menu_always_landscape_portrait = 17301557;
r_drawable_ic_menu_call = 17301558;
r_drawable_ic_menu_camera = 17301559;
r_drawable_ic_menu_close_clear_cancel = 17301560;
r_drawable_ic_menu_compass = 17301561;
r_drawable_ic_menu_crop = 17301562;
r_drawable_ic_menu_day = 17301563;
r_drawable_ic_menu_delete = 17301564;
r_drawable_ic_menu_directions = 17301565;
r_drawable_ic_menu_edit = 17301566;
r_drawable_ic_menu_gallery = 17301567;
r_drawable_ic_menu_help = 17301568;
r_drawable_ic_menu_info_details = 17301569;
r_drawable_ic_menu_manage = 17301570;
r_drawable_ic_menu_mapmode = 17301571;
r_drawable_ic_menu_month = 17301572;
r_drawable_ic_menu_more = 17301573;
r_drawable_ic_menu_my_calendar = 17301574;
r_drawable_ic_menu_mylocation = 17301575;
r_drawable_ic_menu_myplaces = 17301576;
r_drawable_ic_menu_preferences = 17301577;
r_drawable_ic_menu_recent_history = 17301578;
r_drawable_ic_menu_report_image = 17301579;
r_drawable_ic_menu_revert = 17301580;
r_drawable_ic_menu_rotate = 17301581;
r_drawable_ic_menu_save = 17301582;
r_drawable_ic_menu_search = 17301583;
r_drawable_ic_menu_send = 17301584;
r_drawable_ic_menu_set_as = 17301585;
r_drawable_ic_menu_share = 17301586;
r_drawable_ic_menu_slideshow = 17301587;
r_drawable_ic_menu_sort_alphabetically = 17301660;
r_drawable_ic_menu_sort_by_size = 17301661;
r_drawable_ic_menu_today = 17301588;
r_drawable_ic_menu_upload = 17301589;
r_drawable_ic_menu_upload_you_tube = 17301590;
r_drawable_ic_menu_view = 17301591;
r_drawable_ic_menu_week = 17301592;
r_drawable_ic_menu_zoom = 17301593;
r_drawable_ic_notification_clear_all = 17301594;
r_drawable_ic_notification_overlay = 17301595;
r_drawable_ic_partial_secure = 17301596;
r_drawable_ic_popup_disk_full = 17301597;
r_drawable_ic_popup_reminder = 17301598;
r_drawable_ic_popup_sync = 17301599;
r_drawable_ic_search_category_default = 17301600;
r_drawable_ic_secure = 17301601;
r_drawable_list_selector_background = 17301602;
r_drawable_menu_frame = 17301603;
r_drawable_menu_full_frame = 17301604;
r_drawable_menuitem_background = 17301605;
r_drawable_picture_frame = 17301606;
r_drawable_presence_audio_away = 17301679;
r_drawable_presence_audio_busy = 17301680;
r_drawable_presence_audio_online = 17301681;
r_drawable_presence_away = 17301607;
r_drawable_presence_busy = 17301608;
r_drawable_presence_invisible = 17301609;
r_drawable_presence_offline = 17301610;
r_drawable_presence_online = 17301611;
r_drawable_presence_video_away = 17301676;
r_drawable_presence_video_busy = 17301677;
r_drawable_presence_video_online = 17301678;
r_drawable_progress_horizontal = 17301612;
r_drawable_progress_indeterminate_horizontal = 17301613;
r_drawable_radiobutton_off_background = 17301614;
r_drawable_radiobutton_on_background = 17301615;
r_drawable_screen_background_dark = 17301656;
r_drawable_screen_background_dark_transparent = 17301673;
r_drawable_screen_background_light = 17301657;
r_drawable_screen_background_light_transparent = 17301674;
r_drawable_spinner_background = 17301616;
r_drawable_spinner_dropdown_background = 17301617;
r_drawable_star_big_off = 17301619;
r_drawable_star_big_on = 17301618;
r_drawable_star_off = 17301621;
r_drawable_star_on = 17301620;
r_drawable_stat_notify_call_mute = 17301622;
r_drawable_stat_notify_chat = 17301623;
r_drawable_stat_notify_error = 17301624;
r_drawable_stat_notify_missed_call = 17301631;
r_drawable_stat_notify_more = 17301625;
r_drawable_stat_notify_sdcard = 17301626;
r_drawable_stat_notify_sdcard_prepare = 17301675;
r_drawable_stat_notify_sdcard_usb = 17301627;
r_drawable_stat_notify_sync = 17301628;
r_drawable_stat_notify_sync_noanim = 17301629;
r_drawable_stat_notify_voicemail = 17301630;
r_drawable_stat_sys_data_bluetooth = 17301632;
r_drawable_stat_sys_download = 17301633;
r_drawable_stat_sys_download_done = 17301634;
r_drawable_stat_sys_headset = 17301635;
r_drawable_stat_sys_phone_call = 17301636;
r_drawable_stat_sys_phone_call_forward = 17301637;
r_drawable_stat_sys_phone_call_on_hold = 17301638;
r_drawable_stat_sys_speakerphone = 17301639;
r_drawable_stat_sys_upload = 17301640;
r_drawable_stat_sys_upload_done = 17301641;
r_drawable_stat_sys_vp_phone_call = 17301671;
r_drawable_stat_sys_vp_phone_call_on_hold = 17301672;
r_drawable_stat_sys_warning = 17301642;
r_drawable_status_bar_item_app_background = 17301643;
r_drawable_status_bar_item_background = 17301644;
r_drawable_sym_action_call = 17301645;
r_drawable_sym_action_chat = 17301646;
r_drawable_sym_action_email = 17301647;
r_drawable_sym_call_incoming = 17301648;
r_drawable_sym_call_missed = 17301649;
r_drawable_sym_call_outgoing = 17301650;
r_drawable_sym_contact_card = 17301652;
r_drawable_sym_def_app_icon = 17301651;
r_drawable_title_bar = 17301653;
r_drawable_title_bar_tall = 17301670;
r_drawable_toast_frame = 17301654;
r_drawable_zoom_plate = 17301655;
使用以下代码(例如,对于 android 相当于 R.drawable.ic _menu_send):
ntfBuilder.setSmallIcon(r_drawable_ic_menu_send);
因为它在 Android 11 和 Delphi 10.3 上对我不起作用, 我在 System.Notification 和 System.Android.Notification 文件中进行了更改;
(我将它们复制到我的路径并将它们添加到我的应用程序中所以原始文件没问题)
System.Notification 变化:
我向 TNotification 添加了一个新的 public 字段:
SmallIconID : Integer;
并且在构造函数中我用-1初始化;
SmallIconId := -1;
System.Android.Notification 变化:
函数中TNotificationCenterAndroid.CreateNativeNotification 我在 if TOSVersion.Check(5) 行之前添加了这段代码:
if ANotification.SmallIconID > -1 then
Builder := Builder.setSmallIcon(ANotification.SmallIconID);
现在我可以在我的应用程序中设置小图标,它也在 Android 11 上运行。