Intent的用法

来源:互联网 发布:checking php support 编辑:程序博客网 时间:2024/06/07 03:04
Intent的用法:    java.lang.Object     android.content.Intent     public class Intent    extends Object    implements Parcelable, CloneableAn intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity,broadcastIntent to send it to any interested  BroadcastReceiver components,  and Context.startService(android.content.Intent) or  Context.bindService(android.content.Intent,  android.content.ServiceConnection, int) to  communicate with a background Service.  Intent 作用:意图,传输,要做的事 构造方法: 1 public Intent() Create an empty intent. 2 public Intent(String action) Create an intent with a given action. action - The Intent action, such as ACTION_VIEW. for example the system VIEW action is android.intent.action.VIEW;  an application's custom action would be something like  com.google.app.myapp.CUSTOM_ACTION. 3 public Intent(String action, Uri uri) action - The Intent action, such as ACTION_VIEW. uri - The Intent data URI.  例如:Uri uri = Uri.parse(“http://www.google.com”);  Intent it = new Intent(Intent.ACTION_VIEW,uri);  startActivity(it); 发送EmailUri uri = Uri.parse(“mailto:xxx@abc.com”);  Intent it = new Intent(Intent.ACTION_SENDTO, uri);  startActivity(it);  Intent it = new Intent(Intent.ACTION_SEND);  it.putExtra(Intent.EXTRA_EMAIL, “me@abc.com”);  it.putExtra(Intent.EXTRA_TEXT, “The email body text”);  it.setType(“text/plain”);  startActivity(Intent.createChooser(it, “Choose Email Client”));  Intent it=new Intent(Intent.ACTION_SEND);   String[] tos={“me@abc.com”};   String[] ccs={“you@abc.com”};  it.putExtra(Intent.EXTRA_EMAIL, tos);  it.putExtra(Intent.EXTRA_CC, ccs);  it.putExtra(Intent.EXTRA_TEXT, “The email body text”);  it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);  it.setType(“message/rfc822″);  startActivity(Intent.createChooser(it, “Choose Email Client”));   4 public Intent(Context packageContext,      Class<?> cls) 例如  Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivtiy(intent);packageContext - A Context of the application package implementing this class.cls - The component class that is to be used for the intent.5 public Intent(String action,      Uri uri,      Context packageContext,      Class<?> cls)综合了上面1234常用的方法:parseUripublic static Intent parseUri(String uri,              int flags)                       throws URISyntaxExceptionCreate an intent from a URIuri - The URI to turn into an Intent.flags - Additional processing flags. Either 0 or URI_INTENT_SCHEME. setDataAndTypepublic Intent setDataAndType(Uri data,                    String type)data - The Uri of the data this intent is now targeting.type - The MIME type of the data being handled by this intent. 例如:Uri uri = Uri.parse(“file:///sdcard/song.mp3″);  Intent it = new Intent(Intent.ACTION_VIEW, uri);  it.setDataAndType(uri, “audio/mp3″);  context.startActivity(it);  putExtrapublic Intent putExtra(String name,              CharSequence value)name - The name of the extra data, with package prefix.//包名加前缀Intent.EXTRA_TEXTvalue - The CharSequence data value. 例如:it.putExtra(Intent.EXTRA_TEXT, “The email body text”);  或者其他 的也行Intent传递对象:因为Intent传递的类型有限,所以下面的两种方法可以实现自定义传递一个对象第一种是对象实现Serializable:public class Person implements Serializable{    .....}Person person = new Person("Tom",20);Intent intent = new Intent(F.this,S.class);intent.putExtra("person_data",person);startActivity(intent);第二种对象实现Parcelablepublic class Person implements Parcelable{    .....    @Override    public int describeCotents(){        return 0;    }    @Override    public void writeToParcel(Parcel dest,int flags){        dest.writeString(name);//写出Name        dest.writeInt(age);    }public static final Parcelable.Creator<Person> CREATOR =new Parcelable.Creator<Person>(){    @Override    public Person createFromParcel(Parcel source){        Person person = new Person();        person.name = source.readString();//读取name,注意和上面的顺序要一致        person.age = source.readInt();        return person;    }    @Override    public Person[] new Array(int size){        return new Person[size];    }};}用法和上面的Serializable一样读取一个Person对象:Person person = (Person)getIntent().getParcelableEXtra("person_data");Person person = (Person)getIntent().getSerializaleEXtra("person_data");

以后再补充 @_@

0 0
原创粉丝点击