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

标签 android

我一直在尝试寻找一种方法,将消息从设置 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/

相关文章:

android - 如何动态地将图像放入drawable文件夹

android - netstream 无法在 adobe air - amazon s3 - signed cloudfront 上运行

android - 移动视口(viewport)尺寸是否大于屏幕尺寸?

android - 如何通过触摸按钮在android中添加网站链接

java - 如何从我的 Android 应用程序的 firebase 中的键中获取值?

java - 如何在 ListView 中无延迟地从网络下载图像?

android - FutureBuilder 无法正常工作

android - 使用 android Picasso 或 Glide 滚动时出现卡顿效果

android - 通话或短信电话位置

java - 在启动画面期间按主页按钮几秒钟后,Android 应用程序打开