从WallpaperService启动Intent和向WallpaperService启动Intent

时间:2014-01-21 09:01:52

标签: android android-intent android-service live-wallpaper

我有一个显示图像的动态壁纸。我在活动中更改了该图像。然后我需要通知动态壁纸,因此它知道重新加载资源。

意图似乎是完美,简单的解决方案:

Intent intent = new Intent(MyActivity.this, MyWallpaperService.class);
startService(intent);

我和MyWallpaperService

@Override   
public int onStartCommand (Intent intent, int flags, int startId) {...}

我还需要在另一项服务中知道用户点击屏幕的时间。我使用完全相同的发送和接收意图的机制。

一切都在Android 4.0+设备和模拟器上完美运行。 但我在Android 2.2和2.3.3模拟器上测试并得到以下错误:

java.lang.SecurityException: Not allowed to start service Intent { cmp=com.domain.name/.liveWallpaper.MyWallpaperService } without permission android.permission.BIND_WALLPAPER

我的清单在服务标签中包含正确的android:权限:

    <service
        android:name=".liveWallpaper.MyWallpaperService"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_WALLPAPER" >
        <intent-filter>
            <action android:name="android.service.wallpaper.WallpaperService" />
        </intent-filter>
        <meta-data
            android:name="android.service.wallpaper"
            android:resource="@xml/livewallpaper_data" />
    </service>

为什么我只在旧版本(Android 2.2和2.3.3)上出现此错误?是不允许或推荐将Intent发送到WallpaperService?它是否与只有系统可以使用BIND_WALLPAPER权限绑定到服务的事实有关? 最后,如果意图不起作用,那么另一种简单的解决方案是什么?

4 个答案:

答案 0 :(得分:1)

  

java.lang.SecurityException:不允许启动服务Intent {   cmp = com.domain.name / .liveWallpaper.MyWallpaperService}没有   权限android.permission.BIND_WALLPAPER

表示您可能忘记在AndroidManifest.xml内声明BIND_WALLPAPER这样的权限:

 <service
   android:enabled="true"
   android:name="MyWallpaperService"
   android:permission="android.permission.BIND_WALLPAPER" />

答案 1 :(得分:1)

尝试在清单文件中向服务添加权限,如下所示:

<service android:name=".LiveWallpaper"
android:label="@string/app_name"
android:icon="@drawable/icon"
android:permission="android.permission.BIND_WALLPAPER">

希望能帮助

答案 2 :(得分:1)

Android Livewallpaper settings fail to load from 'configure...' menu

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.android.RrD"
  android:versionCode="1"
  android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" />

<uses-feature android:name="android.software.live_wallpaper" />

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BIND_WALLPAPER" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

   <application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    >

    <service android:name=".LiveWallpaper"
        android:label="@string/app_name"
        android:icon="@drawable/icon"
        android:permission="android.permission.BIND_WALLPAPER">

        <intent-filter>
            <action android:name="android.service.wallpaper.WallpaperService" />
        </intent-filter>
        <meta-data android:name="android.service.wallpaper"
            android:resource="@xml/livewallpaper" />

    </service>

<activity android:label="PAM_Prefs"
        android:name=".PAM_Prefs" 

        android:exported="true" android:icon="@drawable/icon">
         <intent-filter>

            <category android:name="android.intent.category.PREFERENCE" />
        </intent-filter>
    </activity>

</application>
</manifest>

答案 3 :(得分:1)

我仍然没有找到解释为什么这不起作用的确切解释,但我找到了一个解决方案:

创建一个将接收我的自定义Intent的BroadcastReceiver。这意味着我将不再使用startService(intent),而是使用sendBroadcast(intent),这不会导致权限问题。 有关更多说明和代码,请参阅参考资料。

参考文献:

https://groups.google.com/forum/#!topic/android-developers/N8TzbbKwd5o

Permission to BIND_WALLPAPER required when messaging WallpaperService from Settings Activity