Android四大组件

Android四大组件

Gerneral

Android开发常用知识储备。Android的四大组件中除了BroadCastReceiver以外,其他三种组件都必须在AndroidManifest中注册
才能使用

Activity

activity

  1. Activity生命周期
  • 完整生存期:活动在onCreate()和onDestroy()方法之间所经历的
  • 可见生存期:活动在onStart()和onStop()方法之间所经历的
  • 交互活动期:活动在onResume()和onPause()方法之间所经历的
  1. Activity注册
    1
    <activity android:name="com.lang.payhelper.SettingActivity"/>
  2. 容易忽略重点
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    @Override
    public void onBackPressed() {
    // sendmsg("触发 onBackPressed"); 界面上的回退
    moveTaskToBack(true);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // 过滤按键动作
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
    sendmsg("触发 KeyEvent.KEYCODE_BACK");
    moveTaskToBack(true);
    }
    return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // 异常恢复
    // TODO:这个还不是很理解
    super.onRestoreInstanceState(savedInstanceState);
    }

Service

Service

  1. 注册service
    1
    2
    3
    4
    5
    <service
    android:name="com.lang.payhelper.payhook.DaemonService"
    android:enabled="true"
    android:exported="false"
    android:process=":daemon_service" />
  2. 启动service方式
  • 本进程 startService(new Intent(this, DaemonService.class));
  • 非本进程 bindService (案例 https://blog.csdn.net/iispring/article/details/48169339
    1
    2
    3
    4
    5
    6
    7
    8
    bindService(intent, new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    }
    @Override
    public void onServiceDisconnected(ComponentName componentName) {
    }
    },BIND_AUTO_CREATE);

BroadcastReceiver

  1. 动态注册 registerReceiver(Received, intentFilter);
  • Received 处理广播的类,继承BroadcastReceiver
  • intentFilter 关注的话题
    1
    2
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BILLRECEIVED_ACTION);
  1. 静态注册
    1
    2
    3
    4
    5
    <receiver android:name=".activity.broadcastreceiver.staticregist.StaticReceiver">
    <intent-filter>
    <action android:name="com.broadcast.static"/>
    </intent-filter>
    </receiver>
  2. 有序广播 priority越小优先执行
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <receiver
    android:name=".activity.broadcastreceiver.orderregist.FirstReceiver">
    <intent-filter android:priority="300">
    <action android:name="com.broadcast.order"/>
    </intent-filter>
    </receiver>
    <receiver
    android:name=".activity.broadcastreceiver.orderregist.SecondReceiver">
    <intent-filter android:priority="200">
    <action android:name="com.broadcast.order"/>
    </intent-filter>
    </receiver>
    <receiver
    android:name=".activity.broadcastreceiver.orderregist.ThirdReveiver">
    <intent-filter android:priority="100">
    <action android:name="com.broadcast.order"/>
    </intent-filter>
    </receiver>
  3. 本地广播

ContentProvider

  1. 原理
    数据通过唯一的 URI 标识来源。ContentProvider 将数据看作表,查询 / 操作数据的时候,通过类似数据库操作的 insert / delete / query / update 方法来实现增删查改操作。因此包含数据提供方和使用方
  2. 提供方 Provider (案例 https://www.jianshu.com/p/ac40ed95d577)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <permission
    android:name="com.straw.providerhost.course.read"
    android:protectionLevel="normal"/>
    <permission
    android:name="com.straw.providerhost.course.write"
    android:protectionLevel="normal"/>

    <application>
    <provider
    android:authorities="com.straw.course"
    android:name="com.straw.providerhost.CourseContentProvider"
    android:exported="true"
    android:readPermission="com.straw.providerhost.course.read"
    android:writePermission="com.straw.providerhost.course.write"/>
    </application>
  3. 使用方 ContentReciver.query(uri)的方式获取数据