我们如何控制 Android 同步适配器首选项?
在尝试编写自定义 Android 同步适配器时,我遵循了此操作。 我成功地使用上述示例中的以下代码片段在常规设置中显示了一个条目(帐户设置)。
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings" />
<PreferenceScreen android:key="account_settings"
android:title="Account Settings" android:summary="Sync frequency, notifications, etc.">
<intent android:action="fm.last.android.activity.Preferences.ACCOUNT_SETUP"
android:targetPackage="fm.last.android"
android:targetClass="fm.last.android.activity.Preferences" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
该代码在常规设置中生成了一个条目(帐户设置):
单击帐户设置后,我收到如下错误,并且设备不必要地重新启动。
错误/AndroidRuntime(30057):android.util.AndroidRuntimeException:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?
我知道这个错误可以通过代码解决。由于“帐户设置”首选项是基于 XML 的代码,因此我遇到了错误。
任何人都可以帮助解决这个问题吗?
我们如何通过代码控制这些类型的首选项?
In an attempt to write a custom Android sync adapter I followed this.
I was success at showing an entry (Account settings) in General setting with the following code snippet from above said example.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General Settings" />
<PreferenceScreen android:key="account_settings"
android:title="Account Settings" android:summary="Sync frequency, notifications, etc.">
<intent android:action="fm.last.android.activity.Preferences.ACCOUNT_SETUP"
android:targetPackage="fm.last.android"
android:targetClass="fm.last.android.activity.Preferences" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
The code resulted me an entry (Account Settings) in General settings:
Upon clicking the Account Settings I'm getting an error as follows and the device reboots unnecessarily.
ERROR/AndroidRuntime(30057): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I know this error can be solved through code. Since "Account Settings" preference is XML-based code I'm stuck with the error.
Can anyone help to solve the Issue?
How do we control these kind of preferences through code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会完全回答您的 2 个问题,但我通过使用以下 3 个步骤解决了这个问题:
设置帐户首选项 XML
我使用的 account_preferences.xml 与 SDK 示例和 c99 Last.fm 应用程序中的非常相似。考虑以下代码片段:
鉴于此,以下是我发现的一些重要要点:(请注意,我是通过实验而不是通过任何特定的 Android 文档找到这些内容的——如果这个问题的未来读者有这些参考资料,那么如果能将这些链接起来就好了。)
创建首选项管理活动
接下来,我创建了一个活动来对应上面 XML 中指定的包和类。请注意,据我所知,Activity 的选择取决于您——最常见的是子类化 android.preference.PreferenceActivity,但我也直接子类化 Activity。标准 Activity 开发指南适用于此处...
从“首选项编辑”意图获取帐户
当您的 Activity 启动时,您可以从 Extras Bundle 中提取相应的 Account 对象(使用 this.getIntent().getExtras())和关键“帐户”。回想一下,该意图将是您最初在首选项 XML 文件中指定的意图。 (同样,我找不到这方面的文档,因此通过转储随我的 Intent 传入的 Extras Bundle 的内容找到了它。)一旦您拥有帐户,就可以使用 SharedPreferences 加载/保存该帐户的首选项,您的数据库,或任何您喜欢的其他方法。
希望有帮助...
I won't exactly answer your 2 questions, but I addressed this problem by working around it using the following 3 steps:
Setting up the account preferences XML
I used an account_preferences.xml very similar to the one in the SDK sample and the c99 Last.fm application. Consider the following snippet:
Given this, here are some of the important points I've found: (Note that I've found these through experimentation and not through any specific Android documentation -- if future readers of this question have those references, it'd be great to link those in.)
Creating the preference managing Activity
Next I created an Activity to correspond to the package and class specified in the above XML. Note that as far as I can tell, the choice of Activity is up to you -- it's most common to subclass android.preference.PreferenceActivity but I've also subclassed Activity directly. Standard Activity development guidelines apply here...
Getting the Account from the "preference editing" Intent
When your Activity starts up, you can extract the corresponding Account object from the Extras Bundle (using this.getIntent().getExtras()) and the key "account". Recall that this Intent will be the one that you specified in preferences XML file initially. (Again, I could not find doc on this so found it by dumping the contents of the Extras Bundle passed in with my Intent.) Once you have the Account, it should be straightforward to load/save preferences for that account using SharedPreferences, your database, or whatever other method you prefer.
Hope that helps...
上面提到的文件/资源不在独立包中:这是作者忘记调整的唯一一件事,我猜:您必须创建自己的首选项类。
这是我的课程:
这里是首选项文件:preferences_resources.xml
您必须调整这些文件,或者更深入地查看他的 last.fm 项目中的文件。
希望这有帮助,祝你好运。
The files/resources refered to above in the are not in the stand alone package : this is the only thing the author forgot to adapt i guess : you have to create your own preference class .
here is my class :
and here is the preferences file : preferences_resources.xml
you will have to adapt those, or have a deeper look at the files in his last.fm project.
hope this helps, good luck.