如果你想为用户设置白日梦,则不能这样做。但是,您可以在正确的位置打开系统设置,以便用户可以从已安装的 Daydreams 中进行选择。
您可以提供一个按钮来访问 Daydream 设置,如下所示:
public void onSettingsButtonClick(View v) {
Intent intent;
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
intent = new Intent(Settings.ACTION_DREAM_SETTINGS);
} else {
intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS);
}
startActivity(intent);
}
这会将用户带到设备设置的“Daydream 设置”或“显示设置”部分。
如果您希望用户能够从设备设置转到特定活动来配置您的 Daydream,您可以添加<meta-data/>
在清单中将此处标记为 Daydream 服务的元素:
<service
android:name="some.package.SomeDaydream"
android:exported="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.service.dreams.DreamService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.service.dream"
android:resource="@xml/dream_info" />
</service>
当定位 api 级别 21 及更高级别时,您必须使用 BIND_DREAM_SERVICE 权限在清单文件中声明该服务。例如:
android:permission="android.permission.BIND_DREAM_SERVICE">
然后,在/res/xml/
, add dream_info.xml
:
<?xml version="1.0" encoding="utf-8"?>
<dream xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="some.package/.SomeActivity" />
我有一个白日梦的例子here https://github.com/adamsp/DreamInTweets显示了这种行为(在两个方向)。