2011年11月8日火曜日

「本体設定」の値の一覧取得プログラム

「本体設定」の値の一覧取得プログラム

「本体設定」の値の一覧を取得するプログラムを作成しました。下記のとおり掲載しておきます。

【このプログラムの特徴】「本体設定」の全ての値を取得できる
例えば、音関係の本体設定情報を取得する場合には、AudioManagerクラスを使います。これはこれで使い易いですが、このような設定情報取得用のクラスが準備されていない設定情報も存在します。例えば、端末メーカー依存の設定情報です。端末メーカー依存の設定情報等は、その存在すら不明です。
下記に掲載したプログラムを使えば、「本体設定」の全ての値を取得できます。
この情報を活用すれば、端末メーカー依存のアプリを開発できます。

【使用対象者等】android programmerが実機で使う
【制約】当然のことながら、利用・活用は自己責任です。
【動作内容】....実は、大したことはやってない。が、実用性はあると思う。
Settings.System及びSettings.Secureを使って取得します。
Settings.Secureは、読み込みはできますが、書き込みはできません。
【参考】
「本体設定」の「音の設定」画面を表示する
ContentResolverで本体設定情報を取得する

public class SettingsActivity extends Activity

    implements
    OnClickListener
    {
    boolean bFlag;
    Button bu;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bu = (Button)findViewById(R.id.Button);
        bu.setOnClickListener(this);
        bFlag = true;
    }
    
    @Override
    public void onClick(View v) {
        if(v==bu) Show();
    }
    
    void Show(){
        String sTV;
        ArrayAdapter<String> aa;
        ListView lv;
        ContentResolver cr;
        Cursor c;
        Uri u;
        String p[];
        TextView tv;
        
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        cr = getContentResolver();
        tv = (TextView)findViewById(R.id.Text);
        if(bFlag==true){
            u =  Settings.System.CONTENT_URI;
            sTV = "Settings.System : ";
            bFlag = false;
        }
        else{
            u =  Settings.Secure.CONTENT_URI;
            sTV = "Settings.Secure : ";
            bFlag = true;
        }
        p = new String[]{
                Settings.NameValueTable.NAME,
                Settings.NameValueTable.VALUE
        };
        c = cr.query(u, p, null, null, null);
        if(c.moveToFirst()==true){
            do{
                String s;
                s = c.getString(c.getColumnIndex(Settings.NameValueTable.NAME));
                s = s + " : " + c.getString(c.getColumnIndex(Settings.NameValueTable.VALUE));
                aa.add(s);
            }while(c.moveToNext()==true);
        }
        aa.sort(new MyComparator());
        sTV = sTV + String.valueOf(c.getCount());
        c.close();
        tv.setText(sTV);
        lv = (ListView)findViewById(R.id.ListView);
        lv.setAdapter(aa);
    }
    
    public class MyComparator implements Comparator<String>{
        @Override
        public int compare(String s1, String s2){
            return s1.compareTo(s2);
        }
    }
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:id="@+id/Button"
        android:text="@string/Button"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        >
    </Button>
    <TextView
        android:id="@+id/Text"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        >
    </TextView>
    <ListView
        android:id="@+id/ListView"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        >
    </ListView>
</LinearLayout>

0 件のコメント:

コメントを投稿