Android基础
一、广播
二、Application类
三、tools:context
四、Android中Bundle类的作用
五、Bundle类的作用
putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。Bundle的内部实际上是使用了HashMap<String, Object>类型的变量来存放putXxx()方法放入的值:
2、Intent附加数据的两种写法
Intentintent = new Intent();
Bundlebundle = new Bundle();//该类用作携带数据
bundle.putString("name","传智播客");
intent.putExtras(bundle);//为意图追加额外的数据,意图原来已经具有的数据不会丢失,但key同名的数据会被替换
第二种写法:这种写法的作用等价于上面的写法,只不过这种写法是把数据一个个地添加进Intent,这种写法使用起来比较方便,而且只需要编写少量的代码。
Intentintent = new Intent();
intent.putExtra("name","XXX");
Intent提供了各种常用类型重载后的putExtra()方法,如: putExtra(String name, String value)、putExtra(String name, long value),在putExtra()方法内部会判断当前Intent对象内部是否已经存在一个Bundle对象,如果不存在就会新建Bundle对象,以后调用putExtra()方法传入的值都会存放于该Bundle对象,
六、notifyDataSetChanged的作用
七、Activity的生命周期和Fragment的生命周期
![](https://images2015.cnblogs.com/blog/1029917/201702/1029917-20170217140652410-689290130.png)
![](https://images2015.cnblogs.com/blog/1029917/201702/1029917-20170217140653300-123835565.png)
1.启动Activity:系统会先调用onCreate方法,然后调用onStart方法,最后调用onResume,Activity进入运行状态。
2.当前Activity被其他Activity覆盖其上或被锁屏:系统会调用onPause方法,暂停当前Activity的执行。
3.当前Activity由被覆盖状态回到前台或解锁屏:系统会调用onResume方法,再次进入运行状态。
4.当前Activity转到新的Activity界面或按Home键回到主屏,自身退居后台:系统会先调用onPause方法,然后调用onStop方法,进入停滞状态。
5.用户后退回到此Activity:系统会先调用onRestart方法,然后调用onStart方法,最后调用onResume方法,再次进入运行状态。
6.当前Activity处于被覆盖状态或者后台不可见状态,即第2步和第4步,系统内存不足,杀死当前Activity,而后用户退回当前Activity:再次调用onCreate方法、onStart方法、onResume方法,进入运行状态。
7.用户退出当前Activity:系统先调用onPause方法,然后调用onStop方法,最后调用onDestory方法,结束当前Activity。
但是知道这些还不够,我们必须亲自试验一下才能深刻体会,融会贯通。
![](https://images2015.cnblogs.com/blog/1029917/201702/1029917-20170217140653988-843565729.png)
![](https://images2015.cnblogs.com/blog/1029917/201702/1029917-20170217140654597-1554962845.png)
![](https://images2015.cnblogs.com/blog/1029917/201702/1029917-20170217140654941-359962727.png)
场景演示 : 切换到该Fragment
11-29 14:26:35.095: D/AppListFragment(7649): onAttach
11-29 14:26:35.095: D/AppListFragment(7649): onCreate
11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
11-29 14:26:35.120: D/AppListFragment(7649): onStart
11-29 14:26:35.120: D/AppListFragment(7649): onResume
锁屏:
11-29 14:27:35.185: D/AppListFragment(7649): onPause
11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
11-29 14:27:35.205: D/AppListFragment(7649): onStop
屏幕解锁
11-29 14:33:13.240: D/AppListFragment(7649): onStart
11-29 14:33:13.275: D/AppListFragment(7649): onResume
切换到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause
11-29 14:33:33.655: D/AppListFragment(7649): onStop
11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView
切换回本身的Fragment:
11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
11-29 14:33:55.825: D/AppListFragment(7649): onStart
11-29 14:33:55.825: D/AppListFragment(7649): onResume
回到桌面
11-29 14:34:26.590: D/AppListFragment(7649): onPause
11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
11-29 14:34:26.880: D/AppListFragment(7649): onStop
回到应用
11-29 14:36:51.940: D/AppListFragment(7649): onStart
11-29 14:36:51.940: D/AppListFragment(7649): onResume
退出应用
11-29 14:37:03.020: D/AppListFragment(7649): onPause
11-29 14:37:03.155: D/AppListFragment(7649): onStop
11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
11-29 14:37:03.165: D/AppListFragment(7649): onDetach
八、Android中的上下文Context
(1)MainActivity.this:表示MainActivity对象,一般用在内部类中指示外面的this,如果在内部类直接用this,指示的是内部类本身。因为MainActivity继承Activity,而Activity继承 Context,所以它也可以用来提供Activity Contex;
(2)this:表示当前对象;一般而言,在哪个类中调用,就是指向该对象。
(3)getContext():这个是View类中提供的方法,在继承了View的类中才可以调用,返回的是当前View运行在哪个Activity Context中。获取的是当前对象的上下文
(4)getActivity和getContext其实差不多,一般在fragment中使用的时候,用此方法获取。。Fragment里边的getActivity()不推荐使用原因如下:这个方法会返回当前Fragment所附加的Activity,当Fragment生命周期结束并销毁时,getActivity()返回的是null,所以在使用时要注意判断null或者捕获空指针异常。所以只要判断getActivity()为空,就可以不再执行下面的代码,这完全不影响业务的使用。
(5).getApplication():获得Application的对象
(6).getApplicationContext():获得应用程序的上下文。有且仅有一个相同的对象。生命周期随着应用程序的摧毁而销毁。就像是社会,所有的都发生在这个社会上,仅且只有一个社 会。每个Activity都有自己的上下文,而整个应用只有一个上下文
4、应用程序创建Context实例的情况有如下几种情况:
1、创建Application 对象时, 而且整个App共一个Application对象
2、创建Service对象时
3、创建Activity对象时
因此应用程序App共有的Context数目公式为:
总Context实例个数 = Service个数 + Activity个数 + 1(Application对应的Context实例)
A.启动Activity
B.启动和停止Service
C.发送广播消息(Intent)
D.注册广播消息(Intent)接收者
可以访问APK中各种资源(如Resources和AssetManager等)
可以访问Package的相关信息
APK的各种权限管理
九、onAttachedToWindow()
十、Android DrawerLayout 点击事件穿透
十一、Android 沉浸式功能,我们项目的顶部和手机的状态栏是融合在一起
@Override
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// API >= 4.4 or API < 5.0 沉浸式状态栏
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
// API >=5.0 沉浸式状态栏
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP){
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
}