Android笔记

移动互联网课考的是安卓。。。作为iOS程序员建立了一下对应关系。。。不知道能不能过=。=

  • Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications.
  • It's possible to arrange for two applications to share the same Linux user ID, in which case they are able to access each other's files. To conserve system resources, applications with the same user ID can also arrange to run in the same Linux process and share the same VM (the applications must also be signed with the same certificate).
  • A central feature of Android is that one application can make use of elements of other applications (provided those applications permit it).
    • Activities
    • Services
    • Broadcast receivers
    • Content providers
  • 安卓能通过Content providers 共享数据,不像iOS只能copy一份
  • Broadcast Receiver 与 iOS 中的NSNotificationCenter略有不同 还能唤醒App真是666,终于知道百度全家桶的由来
  • 安卓中activity有返回值吗?(用别的App拍照返回照片)
    • 有的,通过Intent传回
    • 优点:低耦合
    • 缺点:类型不安全,其实还是存在耦合(需要提前知道类型)
  • 安卓用其它应用的Activity时其实新开了一个进程,那个activity在新开进程里跑
    • For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your application's process. Therefore, unlike applications on most other systems, Android applications don't have a single entry point (there's no main() function, for example)
  • Before the Android system can start an application component, the system must know that the component exists by reading the application's AndroidManifest.xml file (the "manifest" file). Your application must declare all its components in this file, which must be at the root of the application project directory.
  • You can start an activity (or give it something new to do) by passing an Intent to startActivity() or startActivityForResult() (when you want the activity to return a result).

  • You can start a service (or give new instructions to an ongoing service) by passing an Intent to startService(). Or you can bind to the service by passing an Intent to bindService().

  • You can initiate a broadcast by passing an Intent to methods like sendBroadcast(),sendOrderedBroadcast(), or sendStickyBroadcast().

  • You can perform a query to a content provider by calling query() on a ContentResolver.

  • Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run. However, broadcast receivers can be either declared in the manifest or created dynamically in code (as BroadcastReceiver objects) and registered with the system by calling registerReceiver().

  • In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.

  • onStop() is not guaranteed to be called, you can't always do in onStop() what is done in onPause().

  • Be aware that these semantics will change slightly between applications targeting platforms starting with HONEYCOMB vs. those targeting prior platforms. Starting with Honeycomb, an application is not in the killable state until its onStop() has returned. This impacts when onSaveInstanceState(Bundle) may be called (it may be safely called after onPause() and allows and application to safely wait until onStop() to save persistent state.

  • 虽然Activity在功能上与UIViewController相似,但是生命周期的管理却与Application相似(will/did已尽量区分):

    • onCreate(bundle) -> willFinishLaunchingWithOptions(withOptions)
    • onStart() -> applicationWillEnterForeground()
    • onResume() -> applicationWillBecomeActive()
    • onPause() -> applicationWillResignActive()
    • onStop() -> applicationDidEnterForeground()
    • onDestroy() -> applicationWillTerminate()
    • onSaveInstanceState() -> AppDelegate::willEncodeRestorableStateWithCoder() / UIViewController::encodeRestorableStateWithCoder
    • onRestoreInstanceState() -> AppDelegate::didDecodeRestorableState / UIViewController::decodeRestorableStateWithCoder
  • Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.

  • 安卓A activity启动B activity时callback顺序是固定的(在B是本应用同进程情况下), iOS应该完全取决于transition的实现:

    • Activity A's onPause() method executes.
    • Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
    • Then, if Activity A is no longer visible on screen, its onStop() method executes.
  • 安卓Paused和 iOSInactive最大区别是Inactive虽然不接受event,还能执行代码

  • onSaveInstanceState主要用来存储临时状态如scroll position, 而onPause(pre HoneyComb)/onStop 用来存储持久化数据~onSaveInstanceState在旋转时尤为重要

  • Handler -> RunLoopContext(自定义), Looper -> RunLoop, 区别是安卓这边做了更高层的封装直接用Message传递信息给worker thread, 也就是说Message 完成了iOS的约定数据交互处职责和signal runloop职责(刚知道。。。安卓里worker thread不会睡)

    @interface RunLoopContext : NSObject

{

CFRunLoopRef        runLoop;

RunLoopSource*        source;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容