fix: 修复四指点击打开壁纸设置失败的问题

- 使用 ACTION_SET_WALLPAPER 替代 ACTION_ATTACH_DATA
- 添加备用方案:如果 Intent 失败,直接使用 WallpaperManager 设置壁纸
- 添加必要的导入(Bitmap, BitmapFactory, Build)
- 改进错误处理,提供更详细的日志

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-31 22:47:13 +08:00
1 parent de3a36240b
commit 75ecc00c77
1 file changed
+58 -10
@@ -4,12 +4,15 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.RectF; import android.graphics.RectF;
import android.net.Uri; import android.net.Uri;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.service.wallpaper.WallpaperService; import android.service.wallpaper.WallpaperService;
@@ -999,17 +1002,15 @@ public class AutoWallpaperService extends WallpaperService {
if (fixedWallpaperUri != null && !fixedWallpaperUri.isEmpty()) { 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); 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 { 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); startActivity(intent);
// 设置完成后,将 app 状态改为禁用 // 设置完成后,将 app 状态改为禁用
@@ -1019,9 +1020,56 @@ public class AutoWallpaperService extends WallpaperService {
fileLogger.log(TAG, "Fixed wallpaper set to system, app disabled"); fileLogger.log(TAG, "Fixed wallpaper set to system, app disabled");
android.widget.Toast.makeText(AutoWallpaperService.this, "已将固定壁纸设置为系统壁纸,自动更新已禁用", android.widget.Toast.LENGTH_LONG).show(); android.widget.Toast.makeText(AutoWallpaperService.this, "已将固定壁纸设置为系统壁纸,自动更新已禁用", android.widget.Toast.LENGTH_LONG).show();
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Failed to launch wallpaper picker", e); Log.e(TAG, "Failed to launch wallpaper picker with ACTION_SET_WALLPAPER", e);
fileLogger.error(TAG, "Failed to launch wallpaper picker: " + e.getMessage(), e); fileLogger.error(TAG, "ACTION_SET_WALLPAPER failed: " + e.getMessage(), e);
android.widget.Toast.makeText(AutoWallpaperService.this, "打开壁纸设置失败", android.widget.Toast.LENGTH_SHORT).show();
// 方法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 { } else {
// 没有设置固定壁纸,提示用户 // 没有设置固定壁纸,提示用户