从设置活动短信WallpaperService时允许BIND_WALLPAPER要求 [英] Permission to BIND_WALLPAPER required when messaging WallpaperService from Settings Activity

查看:202
本文介绍了从设置活动短信WallpaperService时允许BIND_WALLPAPER要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找到一种方式来传递消息从设置的活动我的壁纸服务。

I've been trying to find a way to pass a message to my Wallpaper Service from a Settings Activity.

在设置我这样做:

Context context = getApplicationContext();

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

context.startService(i);

在我的清单我在意图过滤部分的行动服务

In my Manifest I have the action in the intent filter section for the Service

<action android:name="my_action" />

终于在WallpaperService我已重写 onStartCommand()

当我运行code和呼叫 startService()我得到一个安全异常。

When I run the code and call startService() I get a security exception.

W / ActivityManager(2466):拒绝权限:访问服务
  ComponentInfo {} com.myclassname从PID = 2466,UID = 1000要求
  android.permission.BIND_WALLPAPER

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

因此​​,这似乎是说,我需要给设置对话框允许 BIND_WALLPAPER 。所以,当我补充一点,权限的设置对话框现在崩溃安全性异常。

So this seems to say that I need to give the settings dialog permission to BIND_WALLPAPER. So when I add that permission the settings dialog now crashes with a security exception.

推荐答案

我已经与这个自己奋斗。我发现这个职位是关于这一主题的最有帮助的。
http://groups.google.com/group/android-developers/browse_thread/thread/37c4f36db2b0779a

I've struggled with this myself. I found this post to be the most helpful on this topic. http://groups.google.com/group/android-developers/browse_thread/thread/37c4f36db2b0779a

编辑:刚刚完成这个问题,我已经完成了最终这个任务,我想以同样的方式上述职位的意思(但我不能肯定)。
我的方式做了它是定义一个BroadcastReceiver作为一个内部类我WallpaperService,如下所示(但可能是一个单独的类一样好我猜) -

just to complete this issue, i've eventually accomplished this task, i think in the same way the above post meant it (but i can't be sure). The way i did it is to define a BroadcastReceiver as an inner class of my WallpaperService as follows (but could be a separate class just as well i guess) -

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(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());
            <...>
        }
    }
}

这篇关于从设置活动短信WallpaperService时允许BIND_WALLPAPER要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆