From 75ecc00c7791fc9aabbc505feaf80e7271ecf238 Mon Sep 17 00:00:00 2001 From: hugh <1012760428@qq.com> Date: Sat, 31 Jan 2026 22:47:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9B=9B=E6=8C=87?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E6=89=93=E5=BC=80=E5=A3=81=E7=BA=B8=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 ACTION_SET_WALLPAPER 替代 ACTION_ATTACH_DATA - 添加备用方案:如果 Intent 失败,直接使用 WallpaperManager 设置壁纸 - 添加必要的导入(Bitmap, BitmapFactory, Build) - 改进错误处理,提供更详细的日志 Co-Authored-By: Claude Sonnet 4.5 --- .../gk2/wallpaper/AutoWallpaperService.java | 68 ++++++++++++++++--- 1 file changed, 58 insertions(+), 10 deletions(-) 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 f9ca5e9..a630dde 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 @@ -4,12 +4,15 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.net.Uri; import android.os.BatteryManager; +import android.os.Build; import android.os.Handler; import android.os.Looper; import android.service.wallpaper.WallpaperService; @@ -999,17 +1002,15 @@ public class AutoWallpaperService extends WallpaperService { if (fixedWallpaperUri != null && !fixedWallpaperUri.isEmpty()) { // 有固定壁纸,调用系统壁纸设置 - fileLogger.log(TAG, "Launching system wallpaper picker with fixed wallpaper"); + fileLogger.log(TAG, "Launching system wallpaper picker with fixed wallpaper: " + fixedWallpaperUri); Uri uri = Uri.parse(fixedWallpaperUri); - Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); - intent.addCategory(Intent.CATEGORY_DEFAULT); - intent.setDataAndType(uri, "image/*"); - intent.putExtra("mimeType", "image/*"); - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - try { + // 方法1:使用 ACTION_SET_WALLPAPER(Android 7.0+) + Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); + intent.setDataAndType(uri, "image/*"); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent); // 设置完成后,将 app 状态改为禁用 @@ -1019,9 +1020,56 @@ public class AutoWallpaperService extends WallpaperService { fileLogger.log(TAG, "Fixed wallpaper set to system, app disabled"); android.widget.Toast.makeText(AutoWallpaperService.this, "已将固定壁纸设置为系统壁纸,自动更新已禁用", android.widget.Toast.LENGTH_LONG).show(); } catch (Exception e) { - Log.e(TAG, "Failed to launch wallpaper picker", e); - fileLogger.error(TAG, "Failed to launch wallpaper picker: " + e.getMessage(), e); - android.widget.Toast.makeText(AutoWallpaperService.this, "打开壁纸设置失败", android.widget.Toast.LENGTH_SHORT).show(); + Log.e(TAG, "Failed to launch wallpaper picker with ACTION_SET_WALLPAPER", e); + fileLogger.error(TAG, "ACTION_SET_WALLPAPER failed: " + e.getMessage(), e); + + // 方法2:尝试直接设置壁纸(需要权限) + try { + android.app.WallpaperManager wallpaperManager = + (android.app.WallpaperManager) getSystemService(Context.WALLPAPER_SERVICE); + if (wallpaperManager != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + // Android 7.0+ 可以直接设置壁纸 + java.io.InputStream inputStream = getContentResolver().openInputStream(uri); + if (inputStream != null) { + android.graphics.BitmapFactory.Options options = new android.graphics.BitmapFactory.Options(); + options.inJustDecodeBounds = true; + android.graphics.BitmapFactory.decodeStream(inputStream, null, options); + inputStream.close(); + + // 计算采样率 + int sampleSize = 1; + if (options.outHeight > 2048 || options.outWidth > 2048) { + sampleSize = Math.max(options.outHeight, options.outWidth) / 2048; + } + options.inJustDecodeBounds = false; + options.inSampleSize = sampleSize; + + inputStream = getContentResolver().openInputStream(uri); + android.graphics.Bitmap bitmap = android.graphics.BitmapFactory.decodeStream(inputStream, null, options); + inputStream.close(); + + if (bitmap != null) { + wallpaperManager.setBitmap(bitmap); + bitmap.recycle(); + + // 设置完成后,将 app 状态改为禁用 + config.setFixedWallpaperEnabled(true); + prefs.saveConfig(config); + + fileLogger.log(TAG, "Fixed wallpaper set directly, app disabled"); + android.widget.Toast.makeText(AutoWallpaperService.this, "已将固定壁纸设置为系统壁纸,自动更新已禁用", android.widget.Toast.LENGTH_LONG).show(); + } else { + throw new Exception("Failed to decode bitmap"); + } + } + } else { + throw new Exception("WallpaperManager not available or SDK too old"); + } + } catch (Exception e2) { + Log.e(TAG, "Failed to set wallpaper directly", e2); + fileLogger.error(TAG, "Direct wallpaper set failed: " + e2.getMessage(), e2); + android.widget.Toast.makeText(AutoWallpaperService.this, "打开壁纸设置失败,请手动设置", android.widget.Toast.LENGTH_SHORT).show(); + } } } else { // 没有设置固定壁纸,提示用户