实习笔记-17

协调问题

  1. outsideLauncher

安卓中的单例模式

  • LayoutInflator也是一个SystemService
  • SYSTEM_SERVICE_FETCHERS是一个hashMap,保存ServiceName->ServiceFetcher的单例
  • ServiceFetctor是一个接口,定义了 T getService(ContextImpl ctx);

安卓中的Builder模式

  • 隔离getter,setter,在对象生成时对成员变量配置,生成后屏蔽
  • dialog
阅读更多

实习笔记-19

SlidingPaneLayout

windowSizeClass – 屏幕布局决策

ActivityEmbedding

Box With Constraints – 不同展示内容决策

Custom Layout – 不同方式布局

阅读更多

实习笔记-18

协调问题

  1. 需要SceneEvent的Observer(√)
  2. 跳转问题(√):
    1. battery错误跳转到boost,boost错误跳转到boost
    2. MainPageActivity在任务栈中时,点击通知按钮不跳转到功能页面,没有处理onNewIntent
  3. 在什么位置startService(√)

startActivity的过程

  1. 如果intent指明了Component,直接通过component找到ActivityInfo,否则
  2. 如果Intent指定了组件所在包名,通过包名获取ActivityInfo,否则
  3. 通过ActivityIntentResolver等类的queryIntentForPackage进行模糊匹配,如Action,Category

实习笔记-20

App真正的入口

ActivityThread 中的main方法,一个应用程序对应一个ActivityThread对象,Zygote孵化出一个进程后,就会执行main方法

  • 准备Looper和消息队列
  • thread.attach()方法绑定到ActivityManagerService中

attach方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
private void attach(boolean system, long startSeq) {
sCurrentActivityThread = this;
mConfigurationController = new ConfigurationController(this);
mSystemThread = system;
if (!system) {
android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
UserHandle.myUserId());
RuntimeInit.setApplicationObject(mAppThread.asBinder());
final IActivityManager mgr = ActivityManager.getService();
try {
mgr.attachApplication(mAppThread, startSeq);
//
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
// Watch for getting close to heap limit.
BinderInternal.addGcWatcher(new Runnable() {
@Override public void run() {
if (!mSomeActivitiesChanged) {
return;
}
Runtime runtime = Runtime.getRuntime();
long dalvikMax = runtime.maxMemory();
long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
if (dalvikUsed > ((3*dalvikMax)/4)) {
if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
+ " total=" + (runtime.totalMemory()/1024)
+ " used=" + (dalvikUsed/1024));
mSomeActivitiesChanged = false;
try {
ActivityTaskManager.getService().releaseSomeActivities(mAppThread);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
});
} else {
// Don't set application object here -- if the system crashes,
// we can't display an alert, we just want to die die die.
android.ddm.DdmHandleAppName.setAppName("system_process",
UserHandle.myUserId());
try {
mInstrumentation = new Instrumentation();
mInstrumentation.basicInit(this);
ContextImpl context = ContextImpl.createAppContext(
this, getSystemContext().mPackageInfo);
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
} catch (Exception e) {
throw new RuntimeException(
"Unable to instantiate Application():" + e.toString(), e);
}
}

ViewRootImpl.ConfigChangedCallback configChangedCallback = (Configuration globalConfig) -> {
synchronized (mResourcesManager) {
// TODO (b/135719017): Temporary log for debugging IME service.
if (Build.IS_DEBUGGABLE && mHasImeComponent) {
Log.d(TAG, "ViewRootImpl.ConfigChangedCallback for IME, "
+ "config=" + globalConfig);
}

// We need to apply this change to the resources immediately, because upon returning
// the view hierarchy will be informed about it.
if (mResourcesManager.applyConfigurationToResources(globalConfig,
null /* compat */,
mInitialApplication.getResources().getDisplayAdjustments())) {
mConfigurationController.updateLocaleListFromAppContext(
mInitialApplication.getApplicationContext());

// This actually changed the resources! Tell everyone about it.
final Configuration updatedConfig =
mConfigurationController.updatePendingConfiguration(globalConfig);
if (updatedConfig != null) {
sendMessage(H.CONFIGURATION_CHANGED, globalConfig);
mPendingConfiguration = updatedConfig;
}
}
}
};
ViewRootImpl.addConfigCallback(configChangedCallback);
}

实习笔记-3

在安卓中显示gif图片

使用WebView

1
2
3
4
5
6
<WebView
android:id="@+id/runWebView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
1
2
3
4
5
6
7
8
runWebView.loadDataWithBaseURL(null,
"<html>
<body bgcolor='#f3f3f3'>
<div align=center>
<IMG src='file:///android_asset/run.gif'/>
</div>
</body>
</html>", "text/html", "UTF-8",null);

实现底部状态栏

使用recyclerview + gridlayoutmanager

阅读更多

实习笔记-22

LiveData, MutableLiveData

防止暴露子类某些方法

1
2
val name: LiveData<NameBean> get() = _name
private val _name = MutableLiveData<NameBean>()

界面性能优化ViewStub

根据条件判断某些控件显示,某些不显示时,可以使用ViewStub来减少不必要的实例化开销。

android.view.ViewStub,ViewStub 是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有 ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候。

阅读更多

实习笔记-13

为什么用SurfaceView不用自定义组件

  • 小组件在布局上的局限性
    • 只支持原生控件,且不支持他们的后代
    • 难以动态更新动画
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FrameLayout
LinearLayout
RelativeLayout
GridLayout

AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper
  • 只能显示在某一屏

优化空间

  • 壁纸的操作和部分launcher的操作冲突
  • 只能在右侧,对左撇子不友好
  • 两个wallpaper的drawFrame方法相似,可以进一步抽象
阅读更多

实习笔记-23

MutableLiveData踩坑

使用MutableLiveData的observer对数据进行观察,跳转界面返回后删除list中的元素,出现CurrentModificationException
改用Vector等线程安全的集合

Binder

Binder 与其他IPC的比较

binder 共享内存 Socket
拷贝一次 0 1
C/S模式,易用性高 控制负载,易用性差 C/S开销大
为每个App分配UID 访问接入点是开放的,不安全 访问接入点是开放的,不安全

共享内存 两个mmap,Binder一个mmap

阅读更多