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:[email protected]”);  
Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
startActivity(it);  

Intent it = new Intent(Intent.ACTION_SEND);  
it.putExtra(Intent.EXTRA_EMAIL, “[email protected]”);  
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={“[email protected]”};  
 String[] ccs={“[email protected]”};  
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");

以後再補充 @_@

發佈了33 篇原創文章 · 獲贊 6 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章