我们如何控制一个机器人同步适配器preference?适配器、机器人、preference

2023-09-12 04:55:18 作者:心痛的瞬间

In编写自定义的Andr​​oid同步适配器的尝试,我遵循了这一。 我是在显示在常规设置的条目(帐户设置)成功有以下code段从上面说的例子。

 < preferenceScreen的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    < preferenceCategory机器人:标题=常规设置/>
        < preferenceScreen安卓键=account_settings
             机器人:标题=账户设置安卓总结=同步频率,通知等>
             <意图安卓行动=。fm.last.android.activity preferences.ACCOUNT_SETUP
                 机器人:targetPackage =fm.last.android
                 fm.last.android.activity preferences:机器人targetClass = />
        < / preferenceScreen>
    < / preferenceCategory>
< / preferenceScreen>
 

在code导致我一个条目(帐户设置)的常规设置:

一旦点击帐户设置我发现了一个错误,如下所示,设备重新启动不必要的。

  

ERROR / AndroidRuntime(30057):android.util.AndroidRuntimeException:从活动背景以外调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。难道这真的是你想要的吗?

我知道这个错误可以通过code来解决。由于账户设置preference是基于XML的code我坚持的错误。

谁能帮助解决这个问题?

我们如何通过code控制这些类型的preferences?

解决方案

我会不完全回答你的两个问题,但我通过以下3个步骤围绕它的工作解决了这个问题:

设置了帐户preferences XML 创建活动管理preferences 摘自preference编辑的帐号信息意图

设置帐户preferences XML

我用了一个account_ preferences.xml非常相似的一个SDK的样品和C99 Last.fm应用程序。考虑下面的代码片段:

< preferenceScreen           机器人:关键=account_settings           机器人:标题=账户preferences           机器人:总结=其它帐户preferences>           <意向               机器人:行动=some.unique.action.name.account.EDIT               机器人:targetPackage =com.example preferences               。com.example preferences preferencesActivity:机器人targetClass =>           < /意图> < / preferenceScreen>

鉴于此,这里有一些重要的点,我发现:(请注意,我发现这些通过实验,而不是通过任何特定的Andr​​oid的文件 - 如果这个问题未来的读者有这些引用,它会是伟大的链接那些)

Android的:关键这个preferenceScreen的必须的是account_settings或Android将无法找到和放大器;显示你的preferences 将使用一个明确的意图,并指定targetPackage和targetClass,Android将直接启动你的活动,你并不需要担心的意图过滤器。 Android的商店在此意向的附加当前选定的帐户的帐户的对象 - 这是在接收端很重要,这样你就可以知道哪个帐户你管理。更多关于这下面。

创建preference管理活动

接下来,我创建了一个活动,以对应于上述XML中指定的包和类。需要注意的是,据我所知道的,活动的选择是由你 - 这是最常见的子类的Andr​​oid preference preferenceActivity但我也直接子类的活动。标准活动发展的准则适用于在这里...

从preference编辑获取帐户意图

懒人的福利 小狗智能吸尘器开箱评测

当你的活动启动时,可以提取从其他方案捆绑相应的账户对象(使用this.getIntent()。getExtras())和键账户。回想一下,这种意图将是你在preferences XML文件最初指定的。 (同样,我无法找到这个文档,以便发现倾倒的额外软件包的内容通过与我的意图。)一旦你的帐户,它应该是简单的使用加载/保存preferences该帐户共享preferences,你的数据库,或任何其他方法,你preFER。

希望有所帮助......

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?

解决方案

I won't exactly answer your 2 questions, but I addressed this problem by working around it using the following 3 steps:

Set up Account Preferences XML Create an activity to manage preferences Extract the account information from the "preference editing" Intent

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:

<PreferenceScreen
          android:key="account_settings"
          android:title="Account Preferences"
          android:summary="Misc account preferences">
          <intent
              android:action="some.unique.action.name.account.EDIT"
              android:targetPackage="com.example.preferences"
              android:targetClass="com.example.preferences.PreferencesActivity">
          </intent>
</PreferenceScreen>

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.)

The android:key for this PreferenceScreen must be "account_settings" or Android will not find & display your preferences By using an explicit Intent and specifying the targetPackage and targetClass, android will start your Activity directly and you don't need to worry about an Intent filter. Android stores the Account object for the currently selected account in this Intent's Extras -- which is very important on the receiving end so you can know which account you're managing. More on this below.

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...