4

我正在尝试开发一个内置了白日梦功能的应用程序。我指的是以下教程:

http://www.technotalkative.com/how-to-create-daydream/

在设置>显示>DayDream 下,我可以在应用程序列表中看到我的应用程序,但是当我尝试启动它时,什么也没有发生。我无法理解出了什么问题。

以下是我关于相同清单文件的代码:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <service
        android:name=".MyDreamService"
        android:exported="true"
        android:label="Test - DayDream" >
        <intent-filter>
            <action android:name="android.service.dreams.DreamService" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>
</application>

类文件:

import android.graphics.Color;
import android.service.dreams.DreamService;
import android.widget.TextView;

public class MyDreamService extends DreamService {
    @Override
   public void onAttachedToWindow() {
       super.onAttachedToWindow();

       // Allow user touch
       setInteractive(true);

       // Hide system UI
       setFullscreen(true);

       // Set the dream layout
       TextView txtView = new TextView(this);
       setContentView(txtView);
       txtView.setText("Hello DayDream world from TechnoTalkative.com !!");
       txtView.setTextColor(Color.rgb(184, 245, 0));
       txtView.setTextSize(30);

   }
}
4

1 回答 1

9

您的目标是 api 级别 21 或以上。您需要设置BIND_DREAM_SERVICE服务权限来实现您的白日梦:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
    android:name=".MyDreamService"
    android:exported="true"
    android:label="Test - DayDream" 
    android:permission="android.permission.BIND_DREAM_SERVICE"**>
    <intent-filter>
        <action android:name="android.service.dreams.DreamService" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</service>

于 2016-05-27T20:32:59.763 回答