gpt4 book ai didi

android - 从设置 Activity 发送 WallpaperService 时需要 BIND_WALLPAPER 权限

转载 作者:行者123 更新时间:2023-11-29 22:20:45 24 4
gpt4 key购买 nike

我一直在尝试寻找一种方法,将消息从设置 Activity 传递到我的墙纸服务。

在设置中我这样做:

Context context = getApplicationContext();

Intent i = new Intent(context, RainWallpaper.class);
i.setAction("my_action");

context.startService(i);

在我的 list 中,我在服务的 Intent 过滤器部分中有操作

<action android:name="my_action" />

最后,在 WallpaperService 中,我覆盖了 onStartCommand()

当我运行代码并调用 startService() 时,我得到了一个安全异常。

W/ActivityManager( 2466): Permission Denial: Accessing service ComponentInfo{com.myclassname} from pid=2466, uid=1000 requires android.permission.BIND_WALLPAPER

所以这似乎是说我需要将设置对话框权限授予 BIND_WALLPAPER。因此,当我添加该权限时,设置对话框现在因安全异常而崩溃。

最佳答案

我自己也曾为此苦苦挣扎。我发现这篇文章对这个主题最有帮助。 http://groups.google.com/group/android-developers/browse_thread/thread/37c4f36db2b0779a

编辑:只是为了完成这个问题,我最终完成了这个任务,我认为与上面的帖子的意思相同(但我不能确定)。我这样做的方法是将 BroadcastReceiver 定义为我的 WallpaperService 的内部类,如下所示(但我想也可以是一个单独的类)-

public class MyWallpaperService extends WallpaperService {
private static final String ACTION_PREFIX = MyWallpaperService.class.getName() + ".";

@Override
public Engine onCreateEngine() {
return new <your_engine>;
}

private static void sendAction(Context context, String action) {
Intent intent = new Intent();
intent.setAction(MyWallpaperService.ACTION_PREFIX + action);
context.sendBroadcast(intent);
}

public class WallpaperEngine extends Engine {
private Receiver receiver;

/*****************
* other members *
*****************/

public WallpaperEngine() {
receiver = new Receiver(MyWallpaperService.this);
IntentFilter intentFilter = new IntentFilter();
for (String action: <possible_action_strings>) {
intentFilter.addAction(ACTION_PREFIX + action);
}
registerReceiver(receiver, intentFilter);
}

/****************************
* rest of wallpaper engine *
****************************/

@Override
public void onDestroy() {
<close wallpaper members>
if (receiver != null) {
unregisterReceiver(receiver);
}
receiver = null;
super.onDestroy();
}
}

public static class Receiver extends BroadcastReceiver {
private MyWallpaperService myWallpaper;

public Receiver(MyWallpaperService myWallpaper) {
this.myWallpaper = myWallpaper;
}

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("MyWallpaperService got " + action);
if (!action.startsWith(ACTION_PREFIX)) {
return;
}
String instruction = action.substring(ACTION_PREFIX.length());

/*********************
* rest of the codes *
*********************/
}
}
}

关于android - 从设置 Activity 发送 WallpaperService 时需要 BIND_WALLPAPER 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7298086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com