一、Bundle类:
作用:实现两个activity之间的通讯
用法:
(1)新建一个bundle类
Bundle undle = new Bundle();
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
bundle.putString("Data", "data from TestBundle");
(3)新建一个intent对象,并将该bundle加入这个intent对象
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class); //压入数据
intent.putExtras(bundle);
startActivity(intent);
(4)在另一个类中读出数据
String data = bundle.getString("Data");//读出数据
二、Context
概念:
直译上下文,加载资源、启动一个新的Activity、获取系统服务、获取内部文件(夹)路径、创建View操作时等都需要Context的参与。
Context是个抽象类,Activity、Service、Application都是Context的子类;Context是维持Android程序中各组件能够正常工作的一个核心功能类。
作用:
弹出Toast、启动Activity、启动Service、发送广播、操作数据库等等都需要用到Context。
TextView tv = new TextView(getContext());
ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);getApplicationContext().getSharedPreferences(name, mode);
getApplicationContext().getContentResolver().query(uri, ...);
getContext().getResources().getDisplayMetrics().widthPixels * 5 / 8;
getContext().startActivity(intent);
getContext().startService(intent);
getContext().sendBroadcast(intent);