fix: 使用通知替代直接启动 Activity 打开系统相册

- 由于 Android 系统限制,从 WallpaperService 启动 Activity 被禁止
- 四指点击改为显示通知,用户点击通知后在相册中打开固定壁纸
- 创建通知渠道(Android 8.0+)
- 设置通知优先级为 IMPORTANCE_HIGH
- 同时将 app 设置为禁用

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-31 23:19:23 +08:00
1 parent cfab68718c
commit 2dd338632a
1 file changed
+41 -13
@@ -1,5 +1,8 @@
package cn.hugh.bz.gk2.wallpaper; 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.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -19,6 +22,7 @@ import android.service.wallpaper.WallpaperService;
import android.util.Log; import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import androidx.core.app.NotificationCompat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -972,30 +976,54 @@ public class AutoWallpaperService extends WallpaperService {
String fixedWallpaperUri = config.getFixedWallpaperUri(); String fixedWallpaperUri = config.getFixedWallpaperUri();
if (fixedWallpaperUri != null && !fixedWallpaperUri.isEmpty()) { 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); Uri uri = Uri.parse(fixedWallpaperUri);
try { // 创建通知渠道
// 使用系统相册应用打开固定壁纸 String channelId = "fixed_wallpaper_channel";
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
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 intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*"); intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// 打开相册后,将 app 设置为禁用 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); config.setEnabled(false);
prefs.saveConfig(config); prefs.saveConfig(config);
fileLogger.log(TAG, "Opened gallery, app disabled"); fileLogger.log(TAG, "Notification shown, 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) {
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();
}
} else { } else {
// 没有设置固定壁纸,提示用户 // 没有设置固定壁纸,提示用户
fileLogger.log(TAG, "No fixed wallpaper set, showing hint"); fileLogger.log(TAG, "No fixed wallpaper set, showing hint");