Intent的用法

Intent的用法:

    java.lang.Object 
    android.content.Intent 


    public class Intent
    extends Object
    implements Parcelable, Cloneable


An 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);


 发送Email

Uri 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


常用的方法:
parseUri
public static Intent parseUri(String uri,
              int flags)
                       throws URISyntaxException

Create an intent from a URI
uri - The URI to turn into an Intent.
flags - Additional processing flags. Either 0 or URI_INTENT_SCHEME. 


setDataAndType
public 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);  


putExtra
public Intent putExtra(String name,
              CharSequence value)

name - The name of the extra data, with package prefix.//包名加前缀Intent.EXTRA_TEXT
value - 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);

第二种对象实现Parcelable
public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值