开发者

Permission to BIND_WALLPAPER required when messaging WallpaperService from Settings Activity

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

In the settings I do this:

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" />

Finally in the WallpaperService I have overridden onStartCommand().

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

W/ActivityManager( 2466): Permission Denial: Accessing service ComponentInfo{com.myclassname} from pid=2466, uid=1000 requires android.permission.BIND_WAL开发者_Go百科LPAPER

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.


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

Edit: 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(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 *
             *********************/
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜