2011-09-04 22 views
1

我一直在試圖找到一種方法將消息從設置活動傳遞到我的Wallpaper服務。從設置活動消息傳遞WallpaperService時需要BIND_WALLPAPER的權限

在設置我這樣做:

Context context = getApplicationContext(); 

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

context.startService(i); 

在我的清單我的意圖過濾器部分的動作在WallpaperService我已經覆蓋onStartCommand()服務

<action android:name="my_action" /> 

最後。

當我運行代碼並調用startService()時,出現安全異常。

W/ActivityManager(2466):權限拒絕:訪問服務 ComponentInfo {com.myclassname}從PID = 2466,UID = 1000需要 android.permission.BIND_WALLPAPER

所以這似乎說我需要給設置對話框權限BIND_WALLPAPER。所以,當我添加該權限時,設置對話框現在會崩潰並出現安全異常。

回答

2

我自己一直在努力。我發現這篇文章對這個話題最有幫助。 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(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()); 
      <...> 
     } 
    } 
} 
+0

2年後,這是一個完美的解決方案我需要的東西。謝謝!!一件小事:初始化應該是:receiver = new Receiver(MyWallpaperService.this); – Twinsen