Re: [问题]APP儿童模式与老人模式开发原理

楼主: Jasonzheng (jason)   2016-11-19 10:52:05
您好,假设小弟没有误解您的意思:
您想让按下 home 键时,变成自订主页,并且不出现在总览画面?
这样的功能实现是透过 Activity 没错
并且平常所使用的系统主画面,就是由 Activity 实现的,
点击 app 图示,打开应用,则是透过 startActivity() 送出 intent。
官方原码可以去这边看
https://android.googlesource.com/platform/packages/apps/Launcher3/
在这之前得知道
1. Activity 是搭配 Task 运作, Task 是 ‘相关’ Activity 之集合 ,
且实作堆叠的资料结构 (LIFO) 。
2. Intent 分为 明确式 (Explicit) 与 隐含示 (Implicit),
明示就是有确切的给予目标类别 如 new Intent(this, OtherActivity.class);
其他都是隐含示。
实作开始:
1. 在 AndroidManifest.xml 中,找到想作为主页的 Activity 的 start tag <>,
加入
android:launchMode="singleTask"
android:taskAffinity=""
android:stateNotNeeded="true"
第一行的作用是: 改变 Activity 的 ‘启动模式’,
让 Task 的 底端 保有此 Activity 的实例 。
第二行的作用是: 所有 Activity 默认的 taskAffinity 都是其 Package,
这里设为空字串,代表不隶属于其他 Task
第三行的作用是: 不接收状态
2. 接着, 在该 Activity 的 <intent-filter> </intent-filter> 中,
加入
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
第一行的作用是: 确保该 Activity 能接收到所有的 隐含式 Intent
第二行的作用是: 设定此 Activity 为装置的首页,需搭配前者使用
大概会长这个样子
https://pastebin.com/embed_iframe/wrQCwk5x
希望有帮到你~
参考资料:
https://developer.android.com/reference/android/content/Intent.html
作者: pttnews (PTT新闻)   2016-11-19 20:50:00
好人给推
作者: str5 (chie)   2016-11-20 00:21:00
非常感谢热心的版大! 看完了版大分享的资料之后茅塞顿开

Links booklink

Contact Us: admin [ a t ] ucptt.com