无法在我的自定义安卓应用程序中启动DayDream服务



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

http://www.technotalkative.com/how-to-create-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);
   }
}

您的目标是api级别21或更高。您需要设置BIND_REAM_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>

最新更新