一些面试相关的东西,包括面试题目、技巧等。
面试题
Android
NAR
- 输入事件(按键和触摸事件) 5s 内没被处理: Input event dispatching timed out
BroadcastReceiver 的事件( onRecieve 方法)在规定时间内没处理完(前台广播为 10s,后台广播为 60s):Timeout of broadcast BroadcastRecord
07-27 19:18:47.448 1707 1766 W BroadcastQueue: Receiver during timeout: ResolveInfo{ccd831e com.example.qintong.myapplication/.MyBroadCastReciever m=0x108000} 07-27 19:18:47.502 3513 3728 I WtEventController: ANR com.example.qintong.myapplication 7573
service 前台 20s 后台 200s 未完成启动 Timeout executing service
ContentProvider 的 publish 在 10s 内没进行完:timeout publishing content providers 在android 文档中,只写了第一种和第二种情况,而根据源码和实际的实验,我们能发现 service 的启动和 provider 的 publish 同样会造成 anr 问题。 这里需要注意的是,在后三种情况,以 BroadcastReviever 为例,在 onRecieve()方法执行 10 秒内没发生第一种 ANR(也就是在这个过程中没有输入事件或输入事件还没到 5s)才会发生 Receiver timeout,否则将先发生事件无相应 ANR,所以 onRecieve()是有可能执行不到 10s 就发生 ANR 的,所以不要在 onRecieve()方法里面干活,service 的 onCreate()和 ContentProvider 的 onCreate()也一样,他们都是主线程的,不要在这些方法里干活,这个会在本文最后再细说。
add system service
- 创建
MyService.adil
文件; - 生成
MyService.java
文件; - 添加到
SystemServer
,开机随其开机启动;- 先在
frameworks/base/core/java/android/content/Context.java
中添加一行:public static final String MY_SERVICE = "my_service"
; - 修改
frameworks/base/services/java/com/android/server/SystemServer.java
在 startOtherServices() 函数 的 try 模块中增加以下代码:ServiceManager.addService(Context.MY_SERVICE, new MyService());
- 先在
- 创建
MyManager.java
注册到
SystemService
1 2 3 4 5 6 7 8
registerService(Context.MY_SERVICE, MyManager.class, new CachedServiceFetcher<MyManager>() { @Override public MyManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.MY_SERVICE); IMyService service = IMyService.Stub.asInterface(b); return new MyManager(ctx, service); }});
修改
SePolicy
的编译验证- 修改
system/sepolicy/service.te
在最后一行添加type my_service, system_api_service, system_server_service, service_manager_type;
- 然后修改同目录下 system/sepolicy/service_contexts 文件中间插入一行
my u:object_r:my_service:s0
- 修改
重新编译(
make update-api && make
)
- 创建
SELinux
rule_name source_type target_type : class perm_set
allow installd tmpfs:dir rename;
(installd.te
)[ 27.962493] c3 type=1400 audit(1325820283.567:4): avc: denied { rename } for pid=193 comm=“installd” name=“media” dev=“tmpfs” ino=6449 scontext=u:r:installd:s0 tcontext=u:object_r:tmpfs:s0 tclass=dir
allow installd tmpfs:lnk_file read;
(installd.te
)[ 30.945129] c2 type=1400 audit(1325820286.550:5): avc: denied { read } for pid=427 comm=“installd” name=“0” dev=“tmpfs” ino=6448 scontext=u:r:installd:s0 tcontext=u:object_r:tmpfs:s0 tclass=lnk_file
Recents
Notification
Launcher