まちがいさがし

スマホやガジェットについてなど。技術系メモ以外を中心に。

AccountManagerに登録するアプリで、設定アプリに設定項目を表示させる時の注意

Androidで、他のアプリとかにアカウントを使って欲しい時に、
AccountManagerを使うと思います。

そのためにはAbstractAccountAuthenticatorを実装したりmanifestに宣言したりしますが、、
tappli blog: [Android] アカウント管理を行う


詳しい話は他の人に任せます。
すみません。


それでauthenticator.xmlなどでaccountPreferenceの項目にxmlリソースを設定することで設定アプリ(com.android.settings)に設定画面を表示させることができます。
f:id:wasnot:20140704134423p:plain
でもその指定するxmlリソースは普通のpreferenceをxmlに書くよりすこし注意が必要だったので自分用にメモします。



注意点

  • keyを設定してCheckBoxPreferenceを指定したりしても、設定アプリのpreferenceに保存されるので参照できない
  • ちなみにListPreferenceをつかうとタップした後クラッシュしたりするらしい

android - Account preferences crashes on ListPreference - Stack Overflow

  • なので基本的にintentタグで自分の知ってるアプリなどにIntentを飛ばして、そのActivityなりで設定変更してもらう。つまりリンクを置ける場所。
  • intentタグを含むPreferenceScreenをPreferenceCattegoryの中に置いたりすると

 Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
 って怒られる。
 なのでCategoryは同一階層に何も入れずに並べる
android - PreferenceScreen - <intent .../> - Exception - FLAG_ACTIVITY_NEW_TASK - Stack Overflow

  • key使えないんだからkey入れなくていいだろ!とか思って抜いていたら、PreferenceScreenのTitleに設定アプリのテーマが適応されなくて、テーマが白い端末とかでも文字が白いままで見えなくなった。keyをいれたらtitleは黒くなりました(summaryはグレーの薄いまま。。。)

ということで、模範解答としてGoogleLoginServiceのを参考にしました。

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="プライバシー" />
    <PreferenceScreen android:title="Googleマップ&Latitude" android:key="key1">
        <intent android:action="com.google.android.apps.maps.LOCATION_SETTINGS" />
    </PreferenceScreen>
    <PreferenceScreen android:title="検索" android:key="key2">
        <intent android:action="com.google.android.googlequicksearchbox.action.PRIVACY_SETTINGS" />
    </PreferenceScreen>
    <PreferenceScreen android:title="現在地情報" android:key="key3">
        <intent android:action="com.google.android.gsf.GOOGLE_LOCATION_SETTINGS" />
    </PreferenceScreen>
    <PreferenceScreen android:title="Google+" android:key="key4">
        <intent android:action="com.google.android.apps.plus.PRIVACY_SETTINGS" />
    </PreferenceScreen>
    <PreferenceScreen android:title="広告" android:key="key5">
        <intent android:action="com.google.android.gms.settings.ADS_PRIVACY" />
    </PreferenceScreen>
</PreferenceScreen>

intentタグにはtargetPackageやtargetClassなども指定できるので、明示的Intentも発行できます。

<intent
   android:targetPackage="com.example.myapp"
   android:targetClass="jcom.example.myapp.SettingsActivity"/>