标签:
1)意图,信使
2)值对象(封装数据,实现数据传递)
1)启动组件(activity,service,BroadcastReceiver)
2)停止service,解除receiver的动态注册
3)数据传递(组件之间)
1)封装意图信息(你要做什么)
2)封装数据信息(实现数据传递)
startActivity(intent)
startService(intent)
sendBroadcastReceiver(intent)
意图会通过context对象的相关传递给底层系统,底层根据intent中封装的具体意图信息找到对应的组件,并启动他。
1)显式意图(明确指定要启动的组件)
2)隐式意图(没有明确指定要启动的组件,只是传递字符串给底层系统,底层去匹配对象)
对于一个隐式意图对象常用的配置:(在intent-filter中实现)
1)action(代表具体意图)
2)Category (代表分类,环境)
3)data (数据,可以指定类型)
说明:在使用隐式意图启动activity时,此activity的intent-filter中需要添加一个默认分类(DEFAULT)
隐式意图一般应用于跨app启动其它组件.
扩展:PendingIntent(延迟意图),此对象内部可以封装一个意图对象。
Intent对象其它配置:
1)setFlags(int n):重点关注的是和启动模式相关一些标记
2)setDataAndType(uri,type)
3)......
public class MainActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
//显式意图 public void onClick01(View v){ //startActivity(new Intent(this,OtherActivity01.class)); Intent intent=new Intent(); intent.setClass(this, OtherActivity01.class); 直接类名 //intent.setComponent(new ComponentName(this, OtherActivity01.class));
//startActivity(intent); Bundle options=//封装动画信息 ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left,//可以自己定制 android.R.anim.slide_out_right).toBundle(); startActivity(intent,options); //startActivities(intents) } public void onClick02(View v){ //对于此intent对象在启动activity,底层会自动添加一个分类 Intent intent=new Intent("action.other"); 加入字符串 intent.addCategory("category.other"); intent.setType("text/*"); startActivity(intent); } } |
class Student implements Serializable{ 直接实现接口 private static final long serialVersionUID = -1811479799107069425L; String name; public Student(String name) { this.name=name; } @Override public String toString() { return name; } } class Elephant implements Parcelable{ 实现Parcelable后,重写方法 private String name; private int age; public Elephant(String name,int age) { this.name=name; this.age=age; } //反序列化 public Elephant(Parcel source){ this.name=source.readString(); this.age=source.readInt(); } @Override public String toString() { return name+"/"+age; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } /**序列化*/ @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); } //借助此对象实现反序列化操作 public static final Creator<Elephant> CREATOR= new Creator<Elephant>() { //反序列化 @Override public Elephant createFromParcel(Parcel source) { return new Elephant(source); } @Override public Elephant[] newArray(int size) { // TODO Auto-generated method stub return new Elephant[size]; } }; }
public class MainActivity extends Activity {
private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView=(ImageView) findViewById(R.id.imageView1); } public void onClick01(View v){ Intent intent=new Intent(this,OtherActivity01.class); intent.putExtra("flag01", true); Bundle b=new Bundle();//key/value b.putBoolean("flag02",false); b.putString("flag03", "helloworld"); intent.putExtra("data", b); //可以封装序列化对象 intent.putExtra("student",new Student("陈静")); intent.putExtra("elephant",new Elephant("大象",100)); startActivity(intent); } private Button btn; public void onClick02(View v){ btn=(Button)v; startActivityForResult( new Intent(this,OtherActivity02.class),100);//100表示请求码(标识这是哪个请求) } //通过startActivityForResult方法启动的activity //在关闭时会执行此方法 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.i("TAG", "resultCode="+resultCode); if(requestCode==100&&resultCode==200){ String item=data.getStringExtra("itemKey"); btn.setText(item); }else if(requestCode==101){ Bundle bundle=data.getExtras(); //bundle.keySet(); Bitmap bitMap=(Bitmap)bundle.get("data"); imageView.setImageBitmap(bitMap); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==R.id.cap){ //启动相机程序 startActivityForResult( new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 101); } return super.onOptionsItemSelected(item); } } |
public class OtherActivity02 extends ListActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_other_activity02); setListAdapter(new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, new String[]{"A","B","C"})); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { String item=(String) l.getItemAtPosition(position); Intent intent=new Intent(); intent.putExtra("itemKey", item); setResult(200,//响应码 intent);//设置响应数据 finish(); } } |
public void onClick01(View v){ Intent intent=new Intent(Intent.ACTION_VIEW); intent.setDataAndType( Uri.fromFile( new File("/mnt/sdcard/png_01.png")),"image/*"); startActivity(intent); } |
1)Android 中的一个Context对象(资源访问能力)
2)Android 中的全局访问对象(生命周期同APP生命周期相同)
1)为应用组件提供全局的数据访问(共享数据)
2)记录相应数据信息(不适合大量数据)
例如:
1)用户登录成功以后的用户信息。
2)各个组件都要使用的少量数据信息。
1)编写(继承Application),
2)注册(清单配置文件中指定application的name属性)
3)生命周期(onCreate,......)
说明:我在Application中存储数据也可以直接借助静态的属性存储数据。
public class MyApplication extends Application { public static User user; } |
<application android:name="com.tarena.elts.MyApplication" |
标签:
原文地址:http://blog.csdn.net/gsd1602_yingmu/article/details/51336186