[RE]Android基础

五层架构

(由下向上)

Linux内核:

  • ART依靠linux内核实现底层功能和内存管理
  • linux内核让android更安全
  • 允许制造商开发闭源驱动

硬件抽象层(HAL):

  • 提供标准硬件功能(声音,蓝牙,相机,传感器)
  • 模块化,每个模块实现一个功能
  • 负责统筹和加载底层驱动

原生c/c++库和ART:

库:
  • 核心开源库
  • 使用Java Api访问
ART:
  • 预先和即时编译
  • 垃圾回收
  • 调试支持
  • (>5.0)app在自己的进程中运行,并且有自己的ART实例
  • 核心JAVA库

*框架层:

  • 提供java语言的Android api
  • 各种资源的manager用以管理和操作
  • 版本升级主要是针对框架层

APP层:

  • 核心应用(email\电话\浏览器等)

button添加点击事件:

  • 反射机制实现监听:xml中添加
android:onClick="doClick"​​​​​​​ 

activity中添加

public void onClick(View view){
......
}
  • 内部类或匿名类实现监听器:
  1. 内部类继承OnClickListener
  2. 内部类实现唯一方法onClick()
  3. onCreate()内绑定监听器
  • 外部类实现(同内部类)

intent

  • Manifest intent过滤器:
<intent-filter>
      
      <action android:name="android.intent.action.MAIN" />

      
      <category android:name="android.intent.category.LAUNCHER" />
 intent-filter>
  • intent启动新activity:
  1. 安卓不能直接实例化activity
  2. intent向框架层的activity manager发出请求
  3. android检查intent权限,通过则执行启动

intent启动activity过程:

显式:

Intent intent = new Intent(this,Target.class);
startActivity(intent);

intent可以附加信息:

intent.putExtra("KEY",value);

在新activity中取出信息:

//在新的activity中获取intent引用
Intent intent = getIntent();
//获取KET取出value,可以getint
String str = intent.getStringExtra("KEY");

handler异步通讯机制循环更改ui:

  1. 任务写在Runnable 的run()方法内
  2. 使用post(Runnable r)或postDelayed(r,1000)提交任务
final Handler handler = new Handler();
handler.post(new Runnable(){
@Override
public void run(){
    ......
    //为了定时循环可以使用postDelayed
    handler.postDelayed(this,1000);
}
});

antivity的生命周期:

生命周期方法继承自Activity类
三种状态:

  • launched:被新建未运行
  • running:在前台获得焦点
  • destoryed:被销毁
从生到死的生命周期:

从launched到running到destoryed

三种状态转换时调用生命周期函数。

  1. running前后分别调用onCreate()和onDestoryed();
  2. onCreate()的参数Bundle是一个在线程间传值的工具,使用
savedInstanceState.putInt("seconds",seconds);

等各种类型的put工具存值;使用get得到值

进出停止态:

在launched之后,调用完成onCreate()之后和onDestoryed()之前,onStart()调用完成进入running,onStop()调用完成进入停止态,进入后台挂起,唤醒前先调用onRestart(),再调用onStart()


覆盖生命周期之前必须要先调用super的生命周期


进出暂停:

暂停态:activity失去焦点,停止交互,但仍然可见(分屏模式等)。
拥有最小生命周期循环:
在onStart()和onStop()之间,循环方法为onResume()和onPause()

你可能感兴趣的:([RE]Android基础)