怎么调出手机web serviceService settings界面

6451人阅读
Android FrameWork(5)
笔者前段时间完成设置的圆角item风格的修改,但最近,客户新增需求,想把设置做成Tab风格的,没办法,顾客就是上帝,咱得改啊。今天算是初步改完了,趁着测试紧张测试的空隙,赶紧写写博客,梳理一下,一来是给自己记个笔记,二来希望给有需要的朋友一点帮助。
修改主要是两个点:
①.圆角item的实现。
②.Tab风格的实现。
先来看看下面粗糙的效果图:
我们都知道,Android从3.0开始加入了Fragment,这大大的减少了Activity的使用,在4.0的设置中尤其发挥的淋漓尽致,几乎整个设置应用的一级、二级设置菜单就属于一个Activity即Settings.java。我们能看到不同的设置项,其实就是更换不同的Fragment,表面上是显得很简洁明了,但是修改起来还是有点蛋疼了,一点小改动就有可能牵扯一大部分功能,引起一堆bug。
圆角item的实现:
其实这种圆角的item有两种类型,一种是一直都有的Preference,另外一种是从3.0开始添加的Header。比如设置的第一个主界面使用的就是Header(第一、二张图片所示),其二级、三级菜单就是Preference(第三张图片所示)了,不管是哪种类型,他们都是基于ListView实现的,理解了这个原理,修改起来就简单了,只用在ListView的Adapter的getView函数中,修改对应item项的背景图片即可。观察圆角item的背景图片我们可以发现,先要准备四种背景图片:顶部(上圆下方)、中间(上下都方)、底部(上方下圆)和单独(上下都圆)。
由于Header和Preference还是有一点区别的,Header使用频率没有Preference高,因此,我这里就没有在framework层修改Header,只是在需要用到的地方再修改。所以重点说一下Preference。
在源码中找到:frameworks/base/core/java/android/preference/PreferenceGroupAdapter.java,这个就是Preference的Adapter,首先声明4种类型的背景,然后通过计算得知那一项需要替换成对应的背景图片,最后在getView函数中,把对应的item的背景替换成我们需要的,下面是源码,加上liweiping标签的就是我的修改:
* Copyright (C) 2007 The Android Open Source Project
* Licensed under the Apache License, Version 2.0 (the &License&);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an &AS IS& BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
package android.
import java.util.ArrayL
import java.util.C
import java.util.L
import android.os.H
import android.preference.Preference.OnPreferenceChangeInternalL
import android.view.V
import android.view.ViewG
import android.widget.A
import android.widget.BaseA
import android.widget.ListV
* An adapter that returns the {@link Preference} contained in this group.
* In most cases, this adapter should be the base class for any custom
* adapters from {@link Preference#getAdapter()}.
* This adapter obeys the
* {@link Preference}'s adapter rule (the
* {@link Adapter#getView(int, View, ViewGroup)} should be used instead of
* {@link Preference#getView(ViewGroup)} if a {@link Preference} has an
* adapter via {@link Preference#getAdapter()}).
* This adapter also propagates data change/invalidated notifications upward.
* This adapter does not include this {@link PreferenceGroup} in the returned
* adapter, use {@link PreferenceCategoryAdapter} instead.
* @see PreferenceCategoryAdapter
class PreferenceGroupAdapter extends BaseAdapter implements OnPreferenceChangeInternalListener {
private static final String TAG = &PreferenceGroupAdapter&;
* The group that we are providing data from.
private PreferenceGroup mPreferenceG
* Maps a position into this adapter -& {@link Preference}. These
* {@link Preference}s don't have to be direct children of this
* {@link PreferenceGroup}, they can be grand children or younger)
private List&Preference& mPreferenceL
* List of unique Preference and its subclasses' names. This is used to find
* out how many types of views this adapter can return. Once the count is
* returned, this cannot be modified (since the ListView only checks the
* count once--when the adapter is being set). We will not recycle views for
* Preference subclasses seen after the count has been returned.
private ArrayList&PreferenceLayout& mPreferenceL
private PreferenceLayout mTempPreferenceLayout = new PreferenceLayout();
* Blocks the mPreferenceClassNames from being changed anymore.
private boolean mHasReturnedViewTypeCount =
private volatile boolean mIsSyncing =
private Handler mHandler = new Handler();
//start by liweiping
private ArrayList&Integer& mPreferenceListBackgroundI
private final int SINGLE_LINE_ROUND_CORNER_BACKGROUND = 1;
private final int TOP_ROUND_CORNER_BACKGROUND = 2;
private final int BOTTOM_ROUND_CORNER_BACKGROUND = 3;
private final int CENTER_RECTANGLE_BACKGROUND = 4;
private final int NO_BACKGOUND = 5;
//end by liweiping
private Runnable mSyncRunnable = new Runnable() {
public void run() {
syncMyPreferences();
private static class PreferenceLayout implements Comparable&PreferenceLayout& {
private int resId;
private int widgetResId;
public int compareTo(PreferenceLayout other) {
int compareNames = pareTo(other.name);
if (compareNames == 0) {
if (resId == other.resId) {
if (widgetResId == other.widgetResId) {
return widgetResId - other.widgetResId;
return resId - other.resId;
return compareN
public PreferenceGroupAdapter(PreferenceGroup preferenceGroup) {
mPreferenceGroup = preferenceG
// If this group gets or loses any children, let us know
mPreferenceGroup.setOnPreferenceChangeInternalListener(this);
mPreferenceList = new ArrayList&Preference&();
mPreferenceLayouts = new ArrayList&PreferenceLayout&();
syncMyPreferences();
private void syncMyPreferences() {
synchronized(this) {
if (mIsSyncing) {
mIsSyncing =
List&Preference& newPreferenceList = new ArrayList&Preference&(mPreferenceList.size());
mPreferenceListBackgroundIndex = new ArrayList&Integer&(mPreferenceList.size());//add by liweiping
flattenPreferenceGroup(newPreferenceList, mPreferenceGroup);
mPreferenceList = newPreferenceL
notifyDataSetChanged();
synchronized(this) {
mIsSyncing =
notifyAll();
private void flattenPreferenceGroup(List&Preference& preferences, PreferenceGroup group) {
// TODO: shouldn't always?
group.sortPreferences();
final int groupSize = group.getPreferenceCount();
final int[] tempIndexOfPrefrence
= calcItemsBetweenCategory(group);//add by liweiping
for (int i = 0; i & groupS i++) {
final Preference preference = group.getPreference(i);
preferences.add(preference);
//start by liweiping
if(tempIndexOfPrefrence[i] == 0){
mPreferenceListBackgroundIndex.add(NO_BACKGOUND);
} else if (tempIndexOfPrefrence[i] == 1
&& (i == 0 ? true : tempIndexOfPrefrence[i - 1] &= 1)){
if(i == (groupSize - 1) ? true : tempIndexOfPrefrence[i + 1] &= 1){
mPreferenceListBackgroundIndex.add(SINGLE_LINE_ROUND_CORNER_BACKGROUND);
mPreferenceListBackgroundIndex.add(TOP_ROUND_CORNER_BACKGROUND);
}else if(tempIndexOfPrefrence[i] & 1){
if(i == (groupSize - 1) ? true : tempIndexOfPrefrence[i + 1] &= 1){
mPreferenceListBackgroundIndex.add(BOTTOM_ROUND_CORNER_BACKGROUND);
mPreferenceListBackgroundIndex.add(CENTER_RECTANGLE_BACKGROUND);
//end by liweiping
if (!mHasReturnedViewTypeCount && !preference.hasSpecifiedLayout()) {
addPreferenceClassName(preference);
if (preference instanceof PreferenceGroup) {
final PreferenceGroup preferenceAsGroup = (PreferenceGroup)
if (preferenceAsGroup.isOnSameScreenAsChildren()) {
flattenPreferenceGroup(preferences, preferenceAsGroup);
preference.setOnPreferenceChangeInternalListener(this);
* Creates a string that includes the preference name, layout id and widget layout id.
* If a particular preference type uses 2 different resources, they will be treated as
* different view types.
private PreferenceLayout createPreferenceLayout(Preference preference, PreferenceLayout in) {
PreferenceLayout pl = in != null? in : new PreferenceLayout();
pl.name = preference.getClass().getName();
pl.resId = preference.getLayoutResource();
pl.widgetResId = preference.getWidgetLayoutResource();
private void addPreferenceClassName(Preference preference) {
final PreferenceLayout pl = createPreferenceLayout(preference, null);
int insertPos = Collections.binarySearch(mPreferenceLayouts, pl);
// Only insert if it doesn't exist (when it is negative).
if (insertPos & 0) {
// Convert to insert index
insertPos = insertPos * -1 - 1;
mPreferenceLayouts.add(insertPos, pl);
public int getCount() {
return mPreferenceList.size();
public Preference getItem(int position) {
if (position & 0 || position &= getCount())
return mPreferenceList.get(position);
public long getItemId(int position) {
if (position & 0 || position &= getCount()) return ListView.INVALID_ROW_ID;
return this.getItem(position).getId();
public View getView(int position, View convertView, ViewGroup parent) {
final Preference preference = this.getItem(position);
// Build a PreferenceLayout to compare with known ones that are cacheable.
mTempPreferenceLayout = createPreferenceLayout(preference, mTempPreferenceLayout);
// If it's not one of the cached ones, set the convertView to null so that
// the layout gets re-created by the Preference.
if (Collections.binarySearch(mPreferenceLayouts, mTempPreferenceLayout) & 0) {
convertView =
// start by liweiping
//return preference.getView(convertView, parent);
View mView = preference.getView(convertView, parent);
mView.setBackgroundResource(com.android.internal.R.color.transparent);
if(mPreferenceListBackgroundIndex.get(position) == SINGLE_LINE_ROUND_CORNER_BACKGROUND){
mView.setBackgroundResource(com.android.internal.R.drawable.easy_pref_item_single);
}else if(mPreferenceListBackgroundIndex.get(position) == TOP_ROUND_CORNER_BACKGROUND){
mView.setBackgroundResource(com.android.internal.R.drawable.easy_pref_item_top);
}else if(mPreferenceListBackgroundIndex.get(position) == CENTER_RECTANGLE_BACKGROUND){
mView.setBackgroundResource(com.android.internal.R.drawable.easy_pref_item_center);
}else if(mPreferenceListBackgroundIndex.get(position) == BOTTOM_ROUND_CORNER_BACKGROUND){
mView.setBackgroundResource(com.android.internal.R.drawable.easy_pref_item_bottom);
//end by liweiping
public boolean isEnabled(int position) {
if (position & 0 || position &= getCount())
return this.getItem(position).isSelectable();
public boolean areAllItemsEnabled() {
// There should always be a preference group, and these groups are always
// disabled
public void onPreferenceChange(Preference preference) {
notifyDataSetChanged();
public void onPreferenceHierarchyChange(Preference preference) {
mHandler.removeCallbacks(mSyncRunnable);
mHandler.post(mSyncRunnable);
public boolean hasStableIds() {
public int getItemViewType(int position) {
if (!mHasReturnedViewTypeCount) {
mHasReturnedViewTypeCount =
final Preference preference = this.getItem(position);
if (preference.hasSpecifiedLayout()) {
return IGNORE_ITEM_VIEW_TYPE;
mTempPreferenceLayout = createPreferenceLayout(preference, mTempPreferenceLayout);
int viewType = Collections.binarySearch(mPreferenceLayouts, mTempPreferenceLayout);
if (viewType & 0) {
// This is a class that was seen after we returned the count, so
// don't recycle it.
return IGNORE_ITEM_VIEW_TYPE;
return viewT
public int getViewTypeCount() {
if (!mHasReturnedViewTypeCount) {
mHasReturnedViewTypeCount =
return Math.max(1, mPreferenceLayouts.size());
//start by liweiping
private int[] calcItemsBetweenCategory(PreferenceGroup group){
final int groupSize = group.getPreferenceCount();
int[] indexOfPreference = new int[groupSize];
for (int i = 0; i & groupS i++) {
final Preference preference = group.getPreference(i);
if (preference instanceof PreferenceCategory) {
indexOfPreference[i]
}else if(i == 0){
indexOfPreference[i]
indexOfPreference[i] = indexOfPreference[i - 1] + 1;
return indexOfP
//end by liweiping
tab风格的实现
tab风格的实现有两种方法,一种是传统的TabHost,另外一种就是从3.0开始的ActionBar中的tab,我最开始使用的就是TabHost,也大概实现了,但是最后的Bug让我头疼死了,因此果断放弃,选择了ActionBar的tab,其实一开始就应该选择这个的,由于笔者过度自信对TabHost的使用熟练度,因此结果就是不得不返工,浪费时间精力。其实用ActionBar的tab对代码的改动应该是最小的。下面来看看我修改过后的Settings.java的源码(我的修改也是打过liweiping标签的):
* Copyright (C) 2008 The Android Open Source Project
* Licensed under the Apache License, Version 2.0 (the &License&);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an &AS IS& BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
package com.android.
import com.android.settings.accounts.AccountSyncS
import com.android.settings.bluetooth.BluetoothE
import com.android.settings.fuelgauge.PowerUsageS
import com.android.settings.wifi.WifiE
//import static com.sprd.android.config.OptConfig.LC_RAM_SUPPORT;
import android.app.ActionB
import android.app.ActionBar.T
import android.app.ActionBar.TabL
import android.app.FragmentT
import ponentN
import android.content.C
import android.content.I
import android.content.SharedP
import android.content.pm.ActivityI
import android.content.pm.PackageM
import android.content.pm.PackageManager.NameNotFoundE
import android.os.B
import android.os.SystemP
import android.os.TopwiseP
import android.preference.P
import android.preference.PreferenceA
import android.preference.PreferenceF
import android.preference.PreferenceM
import android.telephony.TelephonyM
import android.text.TextU
import android.util.L
import android.util.SparseA
import android.view.LayoutI
import android.view.V
import android.view.MenuI
import android.view.View.OnClickL
import android.view.ViewG
import android.widget.ArrayA
import android.widget.B
import android.widget.ImageV
import android.widget.ListA
import android.widget.ListV
import android.widget.S
import android.widget.TextV
import java.util.ArrayL
import java.util.HashM
import java.util.L
* Top-level settings activity to handle single pane and double pane UI layout.
public class Settings extends PreferenceActivity implements ButtonBarHandler {
private static final String LOG_TAG = &Settings&;
private static final String META_DATA_KEY_HEADER_ID =
&com.android.settings.TOP_LEVEL_HEADER_ID&;
private static final String META_DATA_KEY_FRAGMENT_CLASS =
&com.android.settings.FRAGMENT_CLASS&;
private static final String META_DATA_KEY_PARENT_TITLE =
&com.android.settings.PARENT_FRAGMENT_TITLE&;
private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
&com.android.settings.PARENT_FRAGMENT_CLASS&;
private static final String EXTRA_CLEAR_UI_OPTIONS = &settings:remove_ui_options&;
private static final String SAVE_KEY_CURRENT_HEADER = &com.android.settings.CURRENT_HEADER&;
private static final String SAVE_KEY_PARENT_HEADER = &com.android.settings.PARENT_HEADER&;
public static boolean UNIVERSEUI_SUPPORT = SystemProperties.getBoolean(&universe_ui_support&,false);
private String mFragmentC
private int mTopLevelHeaderId;
private Header mFirstH
private Header mCurrentH
private Header mParentH
private boolean mInLocalHeaderS
// TODO: Update Call Settings based on airplane mode state.
protected HashMap&Integer, Integer& mHeaderIndexMap = new HashMap&Integer, Integer&();
private List&Header& mH
//start by liweiping
private int mTabF
private static final int FLAG_TAB_WIRELESS_NETWORKS = 0;
private static final int FLAG_TAB_DEVICE = 1;
private static final int FLAG_TAB_PERSONAL = 2;
private static final int FLAG_TAB_SYSTEM = 3;
private final TabListener mTabListener = new TabListener() {
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
public void onTabSelected(Tab tab, FragmentTransaction ft) {
//Log.i(&lwp-log&, &tab.getContentDescription().toString() = &+tab.getContentDescription().toString());
if (tab.getContentDescription().toString().equals(getResources().getString(R.string.header_category_wireless_networks))){
mTabFlag = FLAG_TAB_WIRELESS_NETWORKS;
}else if(tab.getContentDescription().toString().equals(getResources().getString(R.string.header_category_personal))) {
mTabFlag = FLAG_TAB_PERSONAL;
} else if (tab.getContentDescription().toString().equals(getResources().getString(R.string.header_category_system))) {
mTabFlag = FLAG_TAB_SYSTEM;
} else if (tab.getContentDescription().toString().equals(getResources().getString(R.string.header_category_device))) {
mTabFlag = FLAG_TAB_DEVICE;
Log.i(&lwp-log&, &onTabSelected mTabFlag = & + mTabFlag);
invalidateHeaders();
public void onTabReselected(Tab tab, FragmentTransaction ft) {
private void setupWirelessNetworks(ActionBar bar) {
final Tab tab = bar.newTab();
View view = mInflater.inflate(R.layout.tab_widget_view, null);
ImageView dialView =
(ImageView)view.findViewById(R.id.main_activity_tab_image);
if(dialView != null){
dialView.setImageResource(R.drawable.ic_tab_wireless);
TextView dialText = (TextView) view.findViewById(R.id.main_activity_tab_text);
if(dialText!=null){
dialText.setText(R.string.header_category_wireless_networks);
tab.setCustomView(view);
tab.setTabListener(mTabListener);
tab.setContentDescription(R.string.header_category_wireless_networks);
bar.addTab(tab);
private void setupDevice(ActionBar bar) {
final Tab tab = bar.newTab();
View view = mInflater.inflate(R.layout.tab_widget_view, null);
ImageView dialView =
(ImageView)view.findViewById(R.id.main_activity_tab_image);
if(dialView != null){
dialView.setImageResource(R.drawable.ic_tab_device);
TextView dialText = (TextView) view.findViewById(R.id.main_activity_tab_text);
if(dialText!=null){
dialText.setText(R.string.header_category_device);
tab.setCustomView(view);
tab.setTabListener(mTabListener);
tab.setContentDescription(R.string.header_category_device);
bar.addTab(tab);
private void setupPersonal(ActionBar bar) {
final Tab tab = bar.newTab();
View view = mInflater.inflate(R.layout.tab_widget_view, null);
ImageView dialView =
(ImageView)view.findViewById(R.id.main_activity_tab_image);
if(dialView != null){
dialView.setImageResource(R.drawable.ic_tab_personal);
TextView dialText = (TextView) view.findViewById(R.id.main_activity_tab_text);
if(dialText!=null){
dialText.setText(R.string.header_category_personal);
tab.setCustomView(view);
tab.setTabListener(mTabListener);
tab.setContentDescription(R.string.header_category_personal);
bar.addTab(tab);
private void setupSystem(ActionBar bar) {
final Tab tab = bar.newTab();
View view = mInflater.inflate(R.layout.tab_widget_view, null);
ImageView dialView =
(ImageView)view.findViewById(R.id.main_activity_tab_image);
if(dialView != null){
dialView.setImageResource(R.drawable.ic_tab_system);
TextView dialText = (TextView) view.findViewById(R.id.main_activity_tab_text);
if(dialText!=null){
dialText.setText(R.string.header_category_system);
tab.setCustomView(view);
tab.setTabListener(mTabListener);
tab.setContentDescription(R.string.header_category_system);
bar.addTab(tab);
private LayoutInflater mI
//end by liweiping
protected void onCreate(Bundle savedInstanceState) {
if(UNIVERSEUI_SUPPORT){
this.setTheme(R.style.Theme_Holo_new_ui);
if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
getWindow().setUiOptions(0);
//start by liweiping
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mInflater = getLayoutInflater();
mTabFlag = FLAG_TAB_WIRELESS_NETWORKS;
//end by liweiping
getMetaData();
mInLocalHeaderSwitch =
super.onCreate(savedInstanceState);
mInLocalHeaderSwitch =
//For LowCost case, define the list selector by itself
if (LC_RAM_SUPPORT) {
//ListView list = getListView();
//list.setSelector(R.drawable.list_selector_holo_dark);
// list.setOverScrollMode(View.OVER_SCROLL_NEVER);
if (!onIsHidingHeaders() && onIsMultiPane()) {
highlightHeader();
// Force the title so that it doesn't get overridden by a direct launch of
// a specific settings screen.
setTitle(R.string.settings_label);
// Retrieve any saved state
if (savedInstanceState != null) {
mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
// If the current header was saved, switch to it
if (savedInstanceState != null && mCurrentHeader != null) {
//switchToHeaderLocal(mCurrentHeader);
showBreadCrumbs(mCurrentHeader.title, null);
if (mParentHeader != null) {
setParentTitle(mParentHeader.title, null, new OnClickListener() {
public void onClick(View v) {
switchToParent(mParentHeader.fragment);
/* Set ActionBar with popup function */
protected void setActionBarStyle() {
ActionBar actionBar = getActionBar();
if (actionBar == null){
//start by liweiping
Log.i(&lwp-log&, &this.toString() = & + this.toString());
//if ( this.toString().contains(&SubSettings&) ) {
if ( this.toString().contains(&SubSettings&) || this.toString().contains(&$&)) {
//end by liweiping
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP
^ ActionBar.DISPLAY_HOME_AS_UP
, ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(false);
//start by liweiping
actionBar.setAlternativeTabStyle(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
if(actionBar.getTabCount() == 0){
setupWirelessNetworks(actionBar);
setupDevice(actionBar);
setupPersonal(actionBar);
setupSystem(actionBar);
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this);
mTabFlag = settings.getInt(&tab&, FLAG_TAB_WIRELESS_NETWORKS);
Log.i(&lwp-log&, &setActionBarStyle mTabFlag = & + mTabFlag);
actionBar.selectTab(actionBar.getTabAt(mTabFlag));
//end by liweiping
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
// getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
// ActionBar.DISPLAY_HOME_AS_UP);
// Now it's done.
if (item.getItemId() == android.R.id.home) {
return super.onOptionsItemSelected(item);
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the current fragment, if it is the same as originally launched
if (mCurrentHeader != null) {
outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
if (mParentHeader != null) {
outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
public void onResume() {
ListAdapter listAdapter = getListAdapter();
if (listAdapter instanceof HeaderAdapter) {
((HeaderAdapter) listAdapter).resume();
setActionBarStyle();
super.onResume();
public void onPause() {
super.onPause();
ListAdapter listAdapter = getListAdapter();
if (listAdapter instanceof HeaderAdapter) {
((HeaderAdapter) listAdapter).pause();
//start by liweiping
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this);
settings.edit().putInt(&tab&, settings.getInt(&tab&, FLAG_TAB_WIRELESS_NETWORKS)).commit();
//end by liweiping
//start by liweiping
protected void onDestroy() {
super.onDestroy();
//final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this);
//settings.edit().putInt(&tab&, FLAG_TAB_WIRELESS_NETWORKS).commit();
//end by liweiping
public void onBackPressed() {
if (!moveTaskToBack(false)) {
super.onBackPressed();
private void switchToHeaderLocal(Header header) {
mInLocalHeaderSwitch =
switchToHeader(header);
mInLocalHeaderSwitch =
public void switchToHeader(Header header) {
if (!mInLocalHeaderSwitch) {
mCurrentHeader =
mParentHeader =
super.switchToHeader(header);
* Switch to parent fragment and store the grand parent's info
* @param className name of the activity wrapper for the parent fragment.
private void switchToParent(String className) {
final ComponentName cn = new ComponentName(this, className);
final PackageManager pm = getPackageManager();
final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
if (parentInfo != null && parentInfo.metaData != null) {
String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
CharSequence fragmentTitle = parentInfo.loadLabel(pm);
Header parentHeader = new Header();
parentHeader.fragment = fragmentC
parentHeader.title = fragmentT
mCurrentHeader = parentH
switchToHeaderLocal(parentHeader);
highlightHeader();
mParentHeader = new Header();
mParentHeader.fragment
= parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
} catch (NameNotFoundException nnfe) {
Log.w(LOG_TAG, &Could not find parent activity : & + className);
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// If it is not launched from history, then reset to top-level
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
&& mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
switchToHeaderLocal(mFirstHeader);
private void highlightHeader() {
if (mTopLevelHeaderId != 0) {
Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
if (index != null) {
getListView().setItemChecked(index, true);
getListView().smoothScrollToPosition(index);
public Intent getIntent() {
Intent superIntent = super.getIntent();
String startingFragment = getStartingFragmentClass(superIntent);
// This is called from super.onCreate, isMultiPane() is not yet reliable
// Do not use onIsHidingHeaders either, which relies itself on this method
if (startingFragment != null && !onIsMultiPane()) {
Intent modIntent = new Intent(superIntent);
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Bundle args = superIntent.getExtras();
if (args != null) {
args = new Bundle(args);
args = new Bundle();
args.putParcelable(&intent&, superIntent);
modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
return modI
return superI
* Checks if the component name in the intent is different from the Settings class and
* returns the class name to load as a fragment.
protected String getStartingFragmentClass(Intent intent) {
if (mFragmentClass != null) return mFragmentC
String intentClass = intent.getComponent().getClassName();
if (intentClass.equals(getClass().getName()))
if (&com.android.settings.ManageApplications&.equals(intentClass)
|| &com.android.settings.RunningServices&.equals(intentClass)
|| &com.android.settings.applications.StorageUse&.equals(intentClass)) {
// Old names of manage apps.
intentClass = com.android.settings.applications.ManageApplications.class.getName();
return intentC
* Override initial header when an activity-alias is causing Settings to be launched
* for a specific fragment encoded in the android:name parameter.
public Header onGetInitialHeader() {
String fragmentClass = getStartingFragmentClass(super.getIntent());
if (fragmentClass != null) {
Header header = new Header();
header.fragment = fragmentC
header.title = getTitle();
header.fragmentArguments = getIntent().getExtras();
mCurrentHeader =
return mFirstH
public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
int titleRes, int shortTitleRes) {
Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
titleRes, shortTitleRes);
// some fragments want to avoid split actionbar
if (DataUsageSummary.class.getName().equals(fragmentName) ||
PowerUsageSummary.class.getName().equals(fragmentName) ||
AccountSyncSettings.class.getName().equals(fragmentName) ||
UserDictionarySettings.class.getName().equals(fragmentName)) {
intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
intent.setClass(this, SubSettings.class);
* Populate the activity with the top-level headers.
public void onBuildHeaders(List&Header& headers) {
//start by liweiping
//if(UNIVERSEUI_SUPPORT){
loadHeadersFromResource(R.xml.settings_headers_uui, headers);
loadHeadersFromResource(R.xml.settings_headers, headers);
Log.i(&lwp&, &mTabFlag = &+mTabFlag + &, headers.size() = &+ headers.size() );
if (mTabFlag == FLAG_TAB_WIRELESS_NETWORKS){
loadHeadersFromResource(R.xml.settings_headers_wireless_networks, headers);
}else if(mTabFlag == FLAG_TAB_DEVICE){
loadHeadersFromResource(R.xml.settings_headers_device, headers);
}else if(mTabFlag == FLAG_TAB_PERSONAL){
loadHeadersFromResource(R.xml.settings_headers_personal, headers);
loadHeadersFromResource(R.xml.settings_headers_system, headers);
//end by liweiping
updateHeaderList(headers);
//start by liweiping
for bug 1146
//mHeaders =
if(mHeaders != null){
mHeaders.clear();
mHeaders.addAll(headers);
//end by liweiping
private void updateHeaderList(List&Header& target) {
int i = 0;
while (i & target.size()) {
Header header = target.get(i);
// Ids are integers, so downcasting
int id = (int) header.
if (id == R.id.dock_settings) {
if (!needsDockSettings())
target.remove(header);
} else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
} else if (id == R.id.wifi_settings) {
// Remove WiFi Settings if WiFi service is not available.
// modified by zhangguixin
// if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
if (!SystemProperties.getBoolean(&ro.device.support.wifi&, true)) {
// if (&0&.equals(SystemProperties.getBoolean(&ro.device.support.wifi&, &1&))) {
target.remove(header);
} else if (id == R.id.bluetooth_settings) {
// Remove Bluetooth Settings if Bluetooth service is not available.
// modified by zhangguixin
// if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
if (!SystemProperties.getBoolean(&ro.device.support.bt&, true)) {
// if (&0&.equals(SystemProperties.getBoolean(&ro.device.support.bt&, &1&))) {
target.remove(header);
} else if (id == R.id.dual_sim_settings) {
if (!TelephonyManager.isMultiSim()) {
target.remove(header);
} else if (id == R.id.data_usage_settings &&
&sc8825&.equalsIgnoreCase(SystemProperties.get(&ro.board.platform&, &sc8825&))) {//fix bug182500
target.remove(header);
//add by huangweiwei, topwise,
else if (id == R.id.development_settings) {
if (android.os.TopwiseProp.getDefaultSettingBoolean(&settings_remove_development&, false)) {
target.remove(header);
//add end by huangweiwei, topwise,
// Increment if the current one wasn't removed by the Utils code.
if (target.get(i) == header) {
// Hold on to the first header, when we need to reset to the top-level
if (mFirstHeader == null &&
HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
mFirstHeader =
mHeaderIndexMap.put(id, i);
private boolean needsDockSettings() {
return getResources().getBoolean(R.bool.has_dock_settings);
private void getMetaData() {
ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
if (ai == null || ai.metaData == null)
mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
// Check if it has a parent specified and create a Header object
final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
if (parentFragmentClass != null) {
mParentHeader = new Header();
mParentHeader.fragment = parentFragmentC
if (parentHeaderTitleRes != 0) {
mParentHeader.title = getResources().getString(parentHeaderTitleRes);
} catch (NameNotFoundException nnfe) {
// No recovery
public boolean hasNextButton() {
return super.hasNextButton();
public Button getNextButton() {
return super.getNextButton();
private static class HeaderAdapter extends ArrayAdapter&Header& {
static final int HEADER_TYPE_CATEGORY = 0;
static final int HEADER_TYPE_NORMAL = 1;
static final int HEADER_TYPE_SWITCH = 2;
private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
private final WifiEnabler mWifiE
private final BluetoothEnabler mBluetoothE
private SparseArray&View& mViewC
private static class HeaderViewHolder {
Switch switch_;
private LayoutInflater mI
static int getHeaderType(Header header) {
if (header.fragment == null && header.intent == null) {
return HEADER_TYPE_CATEGORY;
} else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
return HEADER_TYPE_SWITCH;
return HEADER_TYPE_NORMAL;
public int getItemViewType(int position) {
Header header = getItem(position);
return getHeaderType(header);
//start by liweiping
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
mViewCache = new SparseArray&View&(getCount());
//end by liweiping
public boolean areAllItemsEnabled() {
// because of categories
public boolean isEnabled(int position) {
return getItemViewType(position) != HEADER_TYPE_CATEGORY;
public int getViewTypeCount() {
return HEADER_TYPE_COUNT;
public boolean hasStableIds() {
public HeaderAdapter(Context context, List&Header& objects) {
super(context, 0, objects);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Temp Switches provided as placeholder until the adapter replaces these with actual
// Switches inflated from their layouts. Must be done before adapter is set in super
mWifiEnabler = new WifiEnabler(context, new Switch(context));
mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
mViewCache = new SparseArray&View&(objects.size());
public View getView(int position, View convertView, ViewGroup parent) {
HeaderViewH
Header header = getItem(position);
int headerType = getHeaderType(header);
View view =
convertView = mViewCache.get(position);
if (convertView == null) {
holder = new HeaderViewHolder();
switch (headerType) {
case HEADER_TYPE_CATEGORY:
view = new TextView(getContext(), null,
android.R.attr.listSeparatorTextViewStyle);
holder.title = (TextView)
case HEADER_TYPE_SWITCH:
view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.title = (TextView)
view.findViewById(com.android.internal.R.id.title);
holder.summary = (TextView)
view.findViewById(com.android.internal.R.id.summary);
holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
case HEADER_TYPE_NORMAL:
view = mInflater.inflate(
com.android.internal.R.layout.preference_header_item, parent,
holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
holder.title = (TextView)
view.findViewById(com.android.internal.R.id.title);
holder.summary = (TextView)
view.findViewById(com.android.internal.R.id.summary);
view.setBackgroundDrawable(null);
view.setTag(holder);
mViewCache.put(position, view);
view = convertV
holder = (HeaderViewHolder) view.getTag();
// All view fields must be updated every time, because the view may be recycled
switch (headerType) {
case HEADER_TYPE_CATEGORY:
holder.title.setText(header.getTitle(getContext().getResources()));
holder.title.setTextColor(android.R.color.transparent);//add by liweiping
case HEADER_TYPE_SWITCH:
// Would need a different treatment if the main menu had more switches
if (header.id == R.id.wifi_settings) {
mWifiEnabler.setSwitch(holder.switch_);
mBluetoothEnabler.setSwitch(holder.switch_);
// No break, fall through on purpose to update common fields
//$FALL-THROUGH$
case HEADER_TYPE_NORMAL:
holder.icon.setImageResource(header.iconRes);
holder.title.setText(header.getTitle(getContext().getResources()));
CharSequence summary = header.getSummary(getContext().getResources());
if (!TextUtils.isEmpty(summary)) {
holder.summary.setVisibility(View.VISIBLE);
holder.summary.setText(summary);
holder.summary.setVisibility(View.GONE);
//start by liweiping
if (header.fragment == null && header.intent == null) {
view.setBackgroundColor(android.R.color.transparent);
}else if(header.id == R.id.dual_sim_settings || header.id == R.id.sound_settings || header.id == R.id.sync_settings || header.id == R.id.dock_settings || header.id == R.id.date_time_settings){
view.setBackgroundResource(com.android.internal.R.drawable.easy_pref_item_top);
}else if(header.id == R.id.wireless_settings || header.id == R.id.manufacturer_settings || header.id == R.id.privacy_settings || header.id == R.id.about_settings || header.id == R.id.application_settings){
view.setBackgroundResource(com.android.internal.R.drawable.easy_pref_item_bottom);
view.setBackgroundResource(com.android.internal.R.drawable.easy_pref_item_center);
//end by liweiping
public void resume() {
mWifiEnabler.resume();
mBluetoothEnabler.resume();
public void pause() {
mWifiEnabler.pause();
mBluetoothEnabler.pause();
public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
// Override the fragment title for Wallpaper settings
int titleRes = pref.getTitleRes();
if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
titleRes = R.string.wallpaper_settings_fragment_
startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, null, null, 0);
//start,added by topwise hehuadong in
public void onHeaderClick(Header header, int position) {
// TODO Auto-generated method stub
//start by liweiping
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this);
settings.edit().putInt(&tab&, mTabFlag).commit();
Log.i(&lwp-log&, &onHeaderClick...& + settings.getInt(&tab&, -1));
//end by liweiping
if (TopwiseProp.getDefaultSettingString(&default_customize_about_device&)!=null){
if (header != null && header.fragment !=null && header.fragment.equals(&com.android.settings.DeviceInfoSettings&)){
header.fragment=&com.android.settings.AboutDeviceSettings&;
super.onHeaderClick(header, position);
//end,added by topwise hehuadong in
public void setListAdapter(ListAdapter adapter) {
if (mHeaders == null) {
mHeaders = new ArrayList&Header&();
// When the saved state provides the list of headers, onBuildHeaders is not called
// Copy the list of Headers from the adapter, preserving their order
for (int i = 0; i & adapter.getCount(); i++) {
mHeaders.add((Header) adapter.getItem(i));
// Ignore the adapter provided by PreferenceActivity and substitute ours instead
super.setListAdapter(new HeaderAdapter(this, mHeaders));
* Settings subclasses for launching independently.
public static class BluetoothSettingsActivity extends Settings { /* empty */ }
public static class WirelessSettingsActivity extends Settings { /* empty */ }
public static class TetherSettingsActivity extends Settings { /* empty */ }
public static class VpnSettingsActivity extends Settings { /* empty */ }
public static class DateTimeSettingsActivity extends Settings { /* empty */ }
public static class StorageSettingsActivity extends Settings { /* empty */ }
public static class WifiSettingsActivity extends Settings { /* empty */ }
public static class WifiP2pSettingsActivity extends Settings { /* empty */ }
public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
public static class SpellCheckersSettingsActivity extends Settings { /* empty */ }
public static class LocalePickerActivity extends Settings { /* empty */ }
public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
public static class SoundSettingsActivity extends Settings { /* empty */ }
public static class DisplaySettingsActivity extends Settings { /* empty */ }
public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
public static class ApplicationSettingsActivity extends Settings { /* empty */ }
public static class ManageApplicationsActivity extends Settings { /* empty */ }
public static class StorageUseActivity extends Settings { /* empty */ }
public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
public static class SecuritySettingsActivity extends Settings { /* empty */ }
public static class LocationSettingsActivity extends Settings { /* empty */ }
public static class PrivacySettingsActivity extends Settings { /* empty */ }
public static class DockSettingsActivity extends Settings { /* empty */ }
public static class RunningServicesActivity extends Settings { /* empty */ }
public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
public static class DataUsageSummaryActivity extends Settings { /* empty */ }
public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
public static class AndroidBeamSettingsActivity extends Settings { /* empty */ }
简单分析一下:
①.首先我们在onResume中找到setActionBarStyle这个函数,这就是设置每次恢复时的必经之路,在这里,我们改变ActionBar的样式,如果是一级界面,就加上tab风格,二级界面就去掉tab风格,显示返回键。
/* Set ActionBar with popup function */
protected void setActionBarStyle() {
ActionBar actionBar = getActionBar();
if (actionBar == null){
//start by liweiping
Log.i(&lwp-log&, &this.toString() = & + this.toString());
//if ( this.toString().contains(&SubSettings&) ) {
if ( this.toString().contains(&SubSettings&) || this.toString().contains(&$&)) {
//end by liweiping
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP
^ ActionBar.DISPLAY_HOME_AS_UP
, ActionBar.DISPLAY_HOME_AS_UP);
actionBar.setDisplayHomeAsUpEnabled(false);
//start by liweiping
actionBar.setAlternativeTabStyle(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
if(actionBar.getTabCount() == 0){
setupWirelessNetworks(actionBar);
setupDevice(actionBar);
setupPersonal(actionBar);
setupSystem(actionBar);
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this);
mTabFlag = settings.getInt(&tab&, FLAG_TAB_WIRELESS_NETWORKS);
Log.i(&lwp-log&, &setActionBarStyle mTabFlag = & + mTabFlag);
actionBar.selectTab(actionBar.getTabAt(mTabFlag));
//end by liweiping
②.监听tab的选择事件,点击每一个tab产生对应的事件,然后把选择项存为全局变量mTabFlag中,最后调用invalidateHeaders()函数刷新界面,替换成不同的Header list。
private final TabListener mTabListener = new TabListener() {
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
public void onTabSelected(Tab tab, FragmentTransaction ft) {
//Log.i(&lwp-log&, &tab.getContentDescription().toString() = &+tab.getContentDescription().toString());
if (tab.getContentDescription().toString().equals(getResources().getString(R.string.header_category_wireless_networks))){
mTabFlag = FLAG_TAB_WIRELESS_NETWORKS;
}else if(tab.getContentDescription().toString().equals(getResources().getString(R.string.header_category_personal))) {
mTabFlag = FLAG_TAB_PERSONAL;
} else if (tab.getContentDescription().toString().equals(getResources().getString(R.string.header_category_system))) {
mTabFlag = FLAG_TAB_SYSTEM;
} else if (tab.getContentDescription().toString().equals(getResources().getString(R.string.header_category_device))) {
mTabFlag = FLAG_TAB_DEVICE;
Log.i(&lwp-log&, &onTabSelected mTabFlag = & + mTabFlag);
invalidateHeaders();
public void onTabReselected(Tab tab, FragmentTransaction ft) {
③.当调用invalidateHeaders()函数时,就进入了onBuildHeaders(List&Header& headers)函数,在这里,我们需要根据mTabFlag来loadHeadersFromResource对应的布局文件,最后一定要记住用新的headers将全局变量mHeaders替换掉。
* Populate the activity with the top-level headers.
public void onBuildHeaders(List&Header& headers) {
//start by liweiping
//if(UNIVERSEUI_SUPPORT){
loadHeadersFromResource(R.xml.settings_headers_uui, headers);
loadHeadersFromResource(R.xml.settings_headers, headers);
Log.i(&lwp&, &mTabFlag = &+mTabFlag + &, headers.size() = &+ headers.size() );
if (mTabFlag == FLAG_TAB_WIRELESS_NETWORKS){
loadHeadersFromResource(R.xml.settings_headers_wireless_networks, headers);
}else if(mTabFlag == FLAG_TAB_DEVICE){
loadHeadersFromResource(R.xml.settings_headers_device, headers);
}else if(mTabFlag == FLAG_TAB_PERSONAL){
loadHeadersFromResource(R.xml.settings_headers_personal, headers);
loadHeadersFromResource(R.xml.settings_headers_system, headers);
//end by liweiping
updateHeaderList(headers);
//start by liweiping
for bug 1146
//mHeaders =
if(mHeaders != null){
mHeaders.clear();
mHeaders.addAll(headers);
//end by liweiping
OK,今天就大概这样了。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1081913次
积分:13816
积分:13816
排名:第695名
原创:160篇
评论:1842条
文章:10篇
阅读:219857
(2)(1)(2)(1)(1)(1)(1)(1)(5)(3)(2)(1)(1)(5)(2)(4)(3)(4)(5)(5)(5)(4)(7)(6)(15)(5)(7)(15)(39)(15)

我要回帖

更多关于 手机web service 的文章

 

随机推荐