您的位置:首页 > 移动开发 > Android开发

Android学习笔记

2016-05-12 14:48 453 查看
1.获得全局Context

全局Application

public class MyApplication extends Application {
private static Context mContext;
private static MyApplication mApp;
@Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
mApp = this;
}

/***
* 获取Context对象
* @return
*/
public static Context getContext(){
return mContext;
}

/****
* 获取Application对象
* @return
*/
public static MyApplication getApp(){
return mApp;
}
}


Activity基类

public class BaseActivity extends AppCompatActivity {
public Context mContext;
public BaseActivity(){
mContext = MyApplication.getContext();
}
}


使用

public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
switch (view.getId()){
case R.id.btn:
Toast.makeText(mContext, "点击了按钮", Toast.LENGTH_SHORT).show();
break;
case R.id.rl_parent:
Toast.makeText(mContext, "点击了相对布局", Toast.LENGTH_SHORT).show();
break;
}
}
}


2.系统启动流程

加载Linux内核—启动init进程—fork出zygote进程—fork出SystemServer进程

在zygote开启的时候,会调用ZygoteInit.main()进行初始化

public static void main(String argv[]) {
......
//为Zygote注册一个服务端Socket,用来与系统服务进行通信
registerZygoteSocket(socketName);
if (argv[1].equals("start-system-server")) {
startSystemServer();
} else if (!argv[1].equals("")) {
throw new RuntimeException(argv[0] + USAGE_STRING);
}
}
......


private static boolean startSystemServer()
throws MethodAndArgsCaller, RuntimeException {
......
int pid;
try {
......
/* fork SystemServer进程*/
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids,
parsedArgs.debugFlags,
null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
} catch (IllegalArgumentException ex) {
throw new RuntimeException(ex);
}
......


public final class SystemServer {

/**
*zygote的入口
*/
public static void main(String[] args) {
new SystemServer().run();
}
// 加载本地服务
System.loadLibrary("android_servers");
// 初始化系统上下文对象
createSystemContext();
}
//创建SystemServiceManager,后面的其他服务都要通过他来启动
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);


private void createSystemContext() {
//创建ActivityThread对象
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
}


public static ActivityThread systemMain() {
if (!ActivityManager.isHighEndGfx()) {
HardwareRenderer.disable(true);
} else {
HardwareRenderer.enableForegroundTrimming();
}
ActivityThread thread = new ActivityThread();
thread.attach(true);
return thread;
}


我们看看attach方法里面

private void attach(boolean system) {
//创建Instrumentation
mInstrumentation = new Instrumentation();
//创建ContextImpl
ContextImpl context = ContextImpl.createAppContext(
this, getSystemContext().mPackageInfo);
//创建Application
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
//调用Application的onCreate方法
mInitialApplication.onCreate();
}


接下来我们继续看SystemService里面,会通过SystemServerManager启动其他服务。

//开启核心服务
private void startBootstrapServices() {

Installer installer = mSystemServiceManager.startService(Installer.class);

// 创建AMS
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);

// 电源管理服务需要早点启动,因为其他服务需要依赖它
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
//让Activity初始化电源管理
mActivityManagerService.initPowerManagement();

// 启动灯光服务
mSystemServiceManager.startService(LightsService.class);
//启动显示服务DMS
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);

// 初始化PMS之前我们需要默认的显示
mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);
....
//启动PMS
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
mFirstBoot = mPackageManagerService.isFirstBoot();
mPackageManager = mSystemContext.getPackageManager();
//启动传感器服务
startSensorService();
}


3.App与AMS通过Binder进行IPC通信,AMS(SystemServer进程)与zygote通过Socket进行IPC通信。在Android系统中,任何一个Activity的启动都是由AMS和应用程序进程(主要是ActivityThread)相互配合来完成的。AMS服务统一调度系统中所有进程的Activity启动,而每个Activity的启动过程则由其所属的进程具体来完成。

4.fitsSystemWindows属性的使用

这个一个boolean值的内部属性,让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为true,就会调整view的paingding属性来给system windows留出空间….

实际效果:

当status bar为透明或半透明时(4.4以上),系统会设置view的paddingTop值为一个适合的值(status bar的高度)让view的内容不被上拉到状态栏,当在不占据status bar的情况下(4.4以下)会设置paddingTop值为0(因为没有占据status bar所以不用留出空间)。


在BaseActivity里面判断

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: