我们如何控制Android同步适配器首选项?
。 我成功地用上面示例中的以下代码片段在常规设置中显示了一个条目(帐户设置)我们如何控制Android同步适配器首选项?,android,android-activity,fatal-error,accountmanager,Android,Android Activity,Fatal Error,Accountmanager,。 我成功地用上面示例中的以下代码片段在常规设置中显示了一个条目(帐户设置) <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="General Settings" /> <PreferenceScreen android:key="account_settings"
<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:从活动上下文外部调用startActivity()需要标志_Activity_NEW_TASK标志。这真的是你想要的吗
我知道这个错误可以通过代码解决。由于“帐户设置”首选项是基于XML的代码,所以我遇到了这个错误
上面在中提到的文件/资源不在独立包中:这是作者唯一忘记修改的东西,我想:您必须创建自己的首选项类。 这是我的班级:
public class AccountPreferences extends PreferenceActivity {
public static final String TAG = "AccountPreferences";
private boolean shouldForceSync = false;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.i(TAG, "onCreate");
addPreferencesFromResource(R.xml.preferences_resources);
@Override
public void onPause() {
super.onPause();
if (shouldForceSync) {
AccountAuthenticatorService.resyncAccount(this);
}
}
Preference.OnPreferenceChangeListener syncToggle = new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
shouldForceSync = true;
return true;
}
};
这是首选项文件:preferences_resources.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/privacy_preferences">
<CheckBoxPreference
android:key="privacy_contacts"
android:defaultValue="true"
android:summary="@string/privacy_contacts_summary" android:title="@string/privacy_contacts_title"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/outgoing_preferences">
<CheckBoxPreference
android:key="allow_mail"
android:defaultValue="true"
android:summary="@string/allow_mail" android:title="@string/allow_mail_text"/>
</PreferenceCategory>
您必须修改这些文件,或者更深入地查看他上一个.fm项目中的文件
希望这有帮助,祝你好运。我不会准确回答你的两个问题,但我通过以下三个步骤解决了这个问题:
鉴于此,以下是我发现的一些要点:(请注意,我是通过实验而不是通过任何特定的Android文档发现的——如果这个问题的未来读者有这些参考资料,最好将它们链接在一起。)
- 此首选项屏幕的安卓:键必须为“帐户设置”,否则安卓将无法找到并显示您的首选项
- 通过使用明确的意图并指定targetPackage和targetClass,android将直接启动您的活动,您无需担心意图过滤器
- Android将当前所选帐户的Account对象存储在此Intent的Extras中,这在接收端非常重要,因此您可以知道您正在管理哪个帐户。下面将对此进行详细介绍
希望这会有所帮助……似乎很多人都在c99上使用这本很棒的教程,但没有多少人会走得更远——唉-