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 { // 没有设置固定壁纸,提示用户