前言
曾听过很多人说Android学习很简单,做个App就上手了,工作机会多,毕业后也比较容易找工作。这种观点可能是很多Android开发者最开始入行的原因之一。
在工作初期,工作主要是按照业务需求实现App页面的功能,按照设计师的设计稿实现页面的效果。
在实现的过程中,总是会被提如下的需求:
这个字能不能大点或者醒目点儿?
颜感觉颜色和设计稿有差别,能不能再调调?
怎么老是崩溃啊,行不行啊?
…
所以,工作过一年后你会发现,自己的工作重点就是**将找各种各样的组件、框架,拖拖拽拽,改改样式。**在极端情况下出现了问题后(比如部分机型上崩溃,加载图片慢),也不知道该如何解决!都不好意思说自己是搞技术的。
造成这种局面的有两种原因:
- 所开发的App功能要求并不复杂,在性能上呢,也不需要极致的追求;
- 自己的意识不够,不知道如何从平凡的工作中提炼可优化的点,在向上提升方面也没有完整的规划。
二,系统设置其他界面
系统设置中除了.homepage.SettingsHomepageActivity
,其他大部分的Activity都定义在Settings
中, 并且继承自SettingsActivity
, 但其中并没有实现任何逻辑。因此,这些Activity的逻辑都是在SettingsActivity
中实现。
/**
- Top-level Settings activity
*/
public class Settings extends SettingsActivity {
/*
- Settings subclasses for launching independently.
/
public static class AssistGestureSettingsActivity extends SettingsActivity { / empty /}
public static class BluetoothSettingsActivity extends SettingsActivity { / empty / }
public static class CreateShortcutActivity extends SettingsActivity { / empty / }
public static class FaceSettingsActivity extends SettingsActivity { / empty / }
public static class FingerprintSettingsActivity extends SettingsActivity { / empty */ }
…
}
这些Activity中并没有实现任何逻辑,那它是怎么加载到自己应有的布局的呢?
在父类SettingsActivity
的onCreate()
中:
@Override
protected void onCreate(Bundle savedState) {
…
// Should happen before any call to getIntent()
// 第一步
getMetaData();
// 第二步
final Intent intent = getIntent();
if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
}
// Getting Intent properties can only be done after the super.onCreate(…)
final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
…
// 第三步
launchSettingFragment(initialFragmentName, intent);
…
}
跟着上面的三个步骤:
第一步
首先通过getMetaData()
获取该Activity在manifest中配置的fragment, 并赋值给mFragmentClass
public static final String META_DATA_KEY_FRAGMENT_CLASS = “com.android.settings.FRAGMENT_CLASS”;
private void getMetaData() {
try {
ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
if (ai == null || ai.metaData == null) return;
mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
} catch (NameNotFoundException nnfe) {
// No recovery
Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
}
}
那么manifest中是怎么配置的呢?如下:
由此可知WifiInfoActivity
这个Acitivity对应的fragment是:com.android.settings.wifi.WifiInfo
第二步
通过getIntent()构造包含EXTRA_SHOW_FRAGMENT
的intent
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) {
Intent modIntent = new Intent(superIntent);
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Bundle args = superIntent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
if (args != null) {
args = new Bundle(args);
} else {
args = new Bundle();
}
args.putParcelable(“intent”, superIntent);
modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
return modIntent;
}
return superIntent;
}
/**
- Checks if the component name in the intent is different from the Settings class and
- returns the class name to load as a fragment.
*/
private String getStartingFragmentClass(Intent intent) {
// 存在mFragmentClass则直接返回
if (mFragmentClass != null) return mFragmentClass;
String intentClass = intent.getComponent().getClassName();
if (intentClass.equals(getClass().getName())) return null;
if (“com.android.settings.RunningServices”.equals(intentClass)
|| “com.android.settings.applications.StorageUse”.equals(intentClass)) {
// Old names of manage apps.
intentClass = ManageApplications.class.getName();
}
return intentClass;
}
这里包含了mFragmentClass
为空的情况,暂时先不管。
第三步
通过launchSettingFragment()
启动对应Fragment,这里的initialFragmentName
参数就是第二步Intent中包含的EXTRA_SHOW_FRAGMENT
参数,mFragmentClass
不为空的情况下传入的就是mFragmentClass
void launchSettingFragment(String initialFragmentName, Intent intent) {
if (initialFragmentName != null) {
setTitleFromIntent(intent);
Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
switchToFragment(initialFragmentName, initialArguments, true,
mInitialTitleResId, mInitialTitle);
} else {
// Show search icon as up affordance if we are displaying the main Dashboard
mInitialTitleResId = R.string.dashboard_title;
switchToFragment(TopLevelSettings.class.getName(), null /* args */, false,
mInitialTitleResId, mInitialTitle);
}
}
在switchToFragment()
中将fragment添加到activity中。
/**
- Switch to a specific Fragment with taking care of validation, Title and BackStack
*/
private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
int titleResId, CharSequence title) {
Log.d(LOG_TAG, "Switching to fragment " + fragmentName);
if (validate && !isValidFragment(fragmentName)) {
throw new IllegalArgumentException("Invalid fragment for this activity: "
- fragmentName);
}
// 反射创建fragment
Fragment f = Utils.getTargetFragment(this, fragmentName, args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.main_content, f);
if (titleResId > 0) {
transaction.setBreadCrumbTitle(titleResId);
} else if (title != null) {
transaction.setBreadCrumbTitle(title);
}
// 提交事务
transaction.commitAllowingStateLoss();
getSupportFragmentManager().executePendingTransactions();
Log.d(LOG_TAG, “Executed frag manager pendingTransactions”);
return f;
}
三,数据控制
在首页-电池设置项中可以显示实时电量。那么它是如果实现的呢?
首先看下它是如何配置的:
题外话
我们见过很多技术leader在面试的时候,遇到处于迷茫期的大龄程序员,比面试官年龄都大。这些人有一些共同特征:可能工作了7、8年,还是每天重复给业务部门写代码,工作内容的重复性比较高,没有什么技术含量的工作。问到这些人的职业规划时,他们也没有太多想法。
其实30岁到40岁是一个人职业发展的黄金阶段,一定要在业务范围内的扩张,技术广度和深度提升上有自己的计划,才有助于在职业发展上有持续的发展路径,而不至于停滞不前。
不断奔跑,你就知道学习的意义所在!
注意:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
的发展路径,而不至于停滞不前。
不断奔跑,你就知道学习的意义所在!
注意:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-yRibLnMc-1649748369942)]
Android开发不会这些?如何面试拿高薪!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)