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:
1 parent
cfab68718c
commit
2dd338632a
1 file changed
+47
-19
@@ -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";
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
NotificationManager notificationManager =
|
||||||
intent.setDataAndType(uri, "image/*");
|
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
startActivity(intent);
|
|
||||||
|
|
||||||
// 打开相册后,将 app 设置为禁用
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
config.setEnabled(false);
|
NotificationChannel channel = new NotificationChannel(
|
||||||
prefs.saveConfig(config);
|
channelId,
|
||||||
|
"固定壁纸",
|
||||||
fileLogger.log(TAG, "Opened gallery, app disabled");
|
NotificationManager.IMPORTANCE_HIGH
|
||||||
android.widget.Toast.makeText(AutoWallpaperService.this, "已在相册中打开固定壁纸,自动更新已禁用", android.widget.Toast.LENGTH_LONG).show();
|
);
|
||||||
} catch (Exception e) {
|
channel.setDescription("在相册中打开固定壁纸");
|
||||||
Log.e(TAG, "Failed to open gallery", e);
|
notificationManager.createNotificationChannel(channel);
|
||||||
fileLogger.error(TAG, "Failed to open gallery: " + e.getMessage(), e);
|
|
||||||
android.widget.Toast.makeText(AutoWallpaperService.this, "打开相册失败", android.widget.Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 创建通知点击 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 {
|
} else {
|
||||||
// 没有设置固定壁纸,提示用户
|
// 没有设置固定壁纸,提示用户
|
||||||
fileLogger.log(TAG, "No fixed wallpaper set, showing hint");
|
fileLogger.log(TAG, "No fixed wallpaper set, showing hint");
|
||||||
|
|||||||
Reference in new issue
Block a user