adw launcher源码阅读(一)配置选项实现与sqlite数据库

来源:互联网 发布:淘宝账号绑定手机 编辑:程序博客网 时间:2024/06/08 15:50

配置选项实现

Preference键值对的方式是一种简单的数据持久化方式,特别是在保存小量的数据时.PreferenceActivity轻松实现一个程序参数设置的UI界面.


arrays.xml定义了一些字符串数组
launcher_settings.xml是设置的界面的定义
MyLauncherSettings.java是设置的界面,包含了界面的处理等
AlmostNexusSettingsHelper.java得到配置参数的值
在AndroidManifest.xml注册launcher_settings这个activity

SharedPreferences数据存储/data/data/PACKAGE_NAME /shared_prefs,在adw里面好像是放在了二进制文件./datacache/data/dalvik-cache/data@app@com.avit.launcher-2.apk@classes.dex.

首先在onCreate里面getSharedPreferences("launcher.preferences.almostnexus", Context.MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this);

也就是当配置改变的时候调用函数onSharedPreferenceChanged.再通过实现onSharedPreferenceChanged来做自己想做的事情.

1.

setSharedPreferencesName("launcher.preferences.almostnexus")
Sets the name of the SharedPreferences file that preferences managed by this will use.

在整个adw中应该有这一个文件launcher.preferences.almostnexus.xml来存放配置数据,但是它没有存放在/data/data/PACKAGE_NAME /shared_prefs下,而是放在了二进制文件/datacache/data/dalvik-cache/data@app@com.avit.launcher-2.apk@classes.dex中

2.

SharedPreferences sp=getPreferenceManager().getSharedPreferences();
Gets a SharedPreferences instance that preferences managed by this will use.
3.
SharedPreferences.Editor editor = sp.edit();editor.putString("themePackageName",packageName);editor.commit();
Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object. 
Note that you must call commit() to have any changes you perform in the Editor actually show up in the SharedPreferences.
4.
SharedPreferences sp = context.getSharedPreferences(ALMOSTNEXUS_PREFERENCES, Context.MODE_PRIVATE);int screens = sp.getInt("desktopScreens", context.getResources().getInteger(R.integer.config_desktopScreens))+1;
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
5.
addPreferencesFromResource(R.xml.launcher_settings);

Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.

从一个xml文件中获取preference然后显示为标准的设置界面

6.
launcher_settings.xml
<PreferenceScreen><PreferenceCategory android:title="@string/pref_title_build_info"><Preferenceandroid:key="adw_restart"android:title="@string/pref_title_adw_restart"android:summary="@string/pref_summary_adw_restart"/></PreferenceCategory>--></PreferenceScreen>
MyLauncherSettings.java
Preference restart=findPreference("adw_restart");restart.setOnPreferenceClickListener(new OnPreferenceClickListener() {public boolean onPreferenceClick(Preference preference) {shouldRestart=true;return false;}});

当某一个选项被按下的时候进行响应.

通过上面的几个函数,就了解了SharedPreferences的操作方法,知道了它到底是怎么的一会事.

通过xml来存放数据是一种方法,但是通过使用sqlite数据库来存放数据也是使用非常多的一种方法.当然adw也使用了.

sqlite数据库

在symbian,android都有对sqlite的封装,sqlite应用广泛.希望自己能在以后对sqlite能进行进一步的研究,它到到底是怎么做到的,我认为是一件很神奇的事情.

在adw中首先由DatabaseHelper的onCreate函数来创建数据库,并由loadFavoritesr把defalut_workspace.xml中的数据存入到数据库中.

把这个数据库拷贝到本机,打开查看下里面的数据.

在这里可以学习到几个操作sqlite的sql命令:

create table创建表

.schema类似mysql的describe,查看表的字段定义

select 查看表中的数据

shell# sqlite3 launcher.db 
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
android_metadata  favorites         gestures   
sqlite> .schema favorites
CREATE TABLE favorites (_id INTEGER PRIMARY KEY,title TEXT,intent TEXT,container INTEGER,screen INTEGER,cellX INTEGER,cellY INTEGER,spanX INTEGER,spanY INTEGER,itemType INTEGER,appWidgetId INTEGER NOT NULL DEFAULT -1,isShortcut INTEGER,iconType INTEGER,iconPackage TEXT,iconResource TEXT,icon BLOB,uri TEXT,displayMode INTEGER);
sqlite> .schema gestures
CREATE TABLE gestures (_id INTEGER PRIMARY KEY,title TEXT,intent TEXT,itemType INTEGER,iconType INTEGER,iconPackage TEXT,iconResource TEXT,icon BLOB);
sqlite> .schema android_metadata
CREATE TABLE android_metadata (locale TEXT);     
sqlite> select * from android_metadata;
md_US
sqlite> select * from favorites;
1|Launcher Actions|#Intent;action=com.avit.launcher.action.launcheraction;launchFlags=0x10000000;component=com.avit.launcher/.CustomShirtcutActivity;i.DefaultLauncherAction.EXTRA_BINDINGVALUE=4;end|-700|-1|-1|-1|1|1|1|-1||0|com.avit.launcher|com.avit.launcher:drawable/all_apps_button|||
2|Settings|#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.android.settings/.Settings;end|-300|-1|-1|-1|1|1|0|-1|||||||
3|Browser|#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.android.browser/.BrowserActivity;end|-400|-1|-1|-1|1|1|0|-1|||||||
4|Launcher Actions|#Intent;action=CustomShirtcutActivity.com.avit.launcher.action.launcheraction;launchFlags=0x10000000;component=com.avit.launcher/.CustomShirtcutActivity;i.DefaultLauncherAction.EXTRA_BINDINGVALUE=4;end|-500|-1|-1|-1|1|1|1|-1||0|com.avit.launcher|com.avit.launcher:drawable/indicator_autocrop|||                                                               
sqlite> select * from gestures;

原创粉丝点击