Android面试大全Interview待读

Android面试大全(四大组件篇)

2017-03-06  本文已影响772人  fengcz

Android面试大全(四大组件篇)
Android面试大全(性能优化篇)
Android面试大全(异常处理篇)
Android面试大全(开源框架篇)
Android面试大全(网络篇)
Android面试大全(java篇)


谈谈对Android四大组件的认识

这都是基本常识


1. Activity

public class Activity
extends ContextThemeWrapper
implements LayoutInflater.Factory2, Window.Callback, KeyEvent.Callback,View.OnCreateContextMenuListener, ComponentCallbacks2

图1-继承关系 图2-activity生命周期
<application
  android:name=".application.FunnyApplication"//注册项目的Application类
        android:allowBackup="true"//可通过adb backup和adb restore来备份和恢复应用程序数据,默认为true;
        android:icon="@mipmap/ic_launcher"//app图标
        android:label="@string/app_name"//app名
        android:supportsRtl="true"//是否支持从右到左布局api 17(4.2时出现)
        android:theme="@style/app_theme">//设置主题
<activity
            android:name=".activity.MainActivity"
            android:configChanges="orientation" />//设置方向
  </application>
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

2.BrocastReceiver

在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件。

下面将详细的阐述如何发送Broadcast和使用BroadcastReceiver过滤接收的过程:

首先在需要发送信息的地方,把要发送的信息和用于过滤的信息(如Action、Category)装入一个Intent对象,然后通过调用 sendOrderBroadcast()或sendStickyBroadcast()方法,把 Intent对象以广播方式发送出去。

当Intent发送以后,所有已经注册的BroadcastReceiver会检查注册时的IntentFilter是否与发送的Intent相匹配,若匹配则就会调用BroadcastReceiver的onReceive()方法。所以当我们定义一个BroadcastReceiver的时候,都需要实现onReceive()方法。

注册BroadcastReceiver有两种方式:

静态注册:在AndroidManifest.xml中用标签注册,并在标签内用标签设置过滤器。

  <receiver android:name="myRecevice">    //继承BroadcastReceiver,重写onReceiver方法

    <intent-filter>    

      <action android:name="com.dragon.net"></action> //使用过滤器,接收指定action广播

      </intent-filter>

  </receiver> 

动态注册:
一般:在onStart中注册,onStop中取消unregisterReceiver

   IntentFilter intentFilter = new IntentFilter();
   intentFilter.addAction(String);   //为BroadcastReceiver指定action,使之用于接收同action的广播
    registerReceiver(BroadcastReceiver,intentFilter);//注册
  Intent intent = new Intent(actionString);//指定广播Action:
  intent.putExtra("msg", "我通过广播发送消息了");//通过Intent携带消息 
  Context.sendBroadcast(intent );//发送广播消息

3. Service

service继承关系

4.ContentProvider

ContentProvider:为存储和获取数据提供统一的接口。可以在不同的应用程序之间共享数据。Android已经为常见的一些数据提供了默认的ContentProvider
 1、ContentProvider使用表的形式来组织数据
   无论数据的来源是什么,ContentProvider都会认为是一种表,然后把数据组织成表格
  2、ContentProvider提供的方法
   query:查询
   insert:插入
   update:更新
   delete:删除
   getType:得到数据类型
   onCreate:创建数据时调用的回调函数
  3、每个ContentProvider都有一个公共的URI,这个URI用于表示这个ContentProvider所提供的数据。Android所提供的ContentProvider都存放在android.provider包当中


正在持续更新中……

上一篇下一篇

猜你喜欢

热点阅读