diff --git a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java index d9c3c47..9c8075f 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java +++ b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java @@ -1,5 +1,8 @@ package cn.hugh.bz.gk2.wallpaper; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -19,6 +22,7 @@ import android.service.wallpaper.WallpaperService; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; +import androidx.core.app.NotificationCompat; import java.util.ArrayList; import java.util.List; @@ -972,30 +976,54 @@ public class AutoWallpaperService extends WallpaperService { String fixedWallpaperUri = config.getFixedWallpaperUri(); if (fixedWallpaperUri != null && !fixedWallpaperUri.isEmpty()) { - // 有固定壁纸,使用系统相册打开 - fileLogger.log(TAG, "Opening fixed wallpaper in system gallery: " + fixedWallpaperUri); + // 有固定壁纸,显示通知 + fileLogger.log(TAG, "Showing notification for fixed wallpaper: " + fixedWallpaperUri); Uri uri = Uri.parse(fixedWallpaperUri); - try { - // 使用系统相册应用打开固定壁纸 - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(uri, "image/*"); - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); + // 创建通知渠道 + String channelId = "fixed_wallpaper_channel"; + NotificationManager notificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - // 打开相册后,将 app 设置为禁用 - config.setEnabled(false); - prefs.saveConfig(config); - - fileLogger.log(TAG, "Opened gallery, app disabled"); - android.widget.Toast.makeText(AutoWallpaperService.this, "已在相册中打开固定壁纸,自动更新已禁用", android.widget.Toast.LENGTH_LONG).show(); - } catch (Exception e) { - Log.e(TAG, "Failed to open gallery", e); - fileLogger.error(TAG, "Failed to open gallery: " + e.getMessage(), e); - android.widget.Toast.makeText(AutoWallpaperService.this, "打开相册失败", android.widget.Toast.LENGTH_SHORT).show(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel( + channelId, + "固定壁纸", + NotificationManager.IMPORTANCE_HIGH + ); + channel.setDescription("在相册中打开固定壁纸"); + notificationManager.createNotificationChannel(channel); } + + // 创建通知点击 Intent + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(uri, "image/*"); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + PendingIntent pendingIntent = PendingIntent.getActivity( + AutoWallpaperService.this, 0, intent, + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); + + // 创建通知 + NotificationCompat.Builder builder = new NotificationCompat.Builder(AutoWallpaperService.this, channelId) + .setSmallIcon(android.R.drawable.ic_menu_gallery) + .setContentTitle("打开固定壁纸") + .setContentText("点击在系统相册中查看固定壁纸") + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setAutoCancel(true) + .setContentIntent(pendingIntent); + + // 显示通知 + notificationManager.notify(1, builder.build()); + + // 将 app 设置为禁用 + config.setEnabled(false); + prefs.saveConfig(config); + + fileLogger.log(TAG, "Notification shown, app disabled"); + android.widget.Toast.makeText(AutoWallpaperService.this, "已发送通知,请在通知栏中点击打开相册", android.widget.Toast.LENGTH_LONG).show(); } else { // 没有设置固定壁纸,提示用户 fileLogger.log(TAG, "No fixed wallpaper set, showing hint");