4Intent物件簡介

鴨脖發表於2012-07-14
Intent物件中包含以下資料資訊:


action The general action to be performed
data The data to operate on
component   Specifies an explicit name of a component class to use for the intent
extras This is a Bundle(鍵值對,另一個activity裡面取資料的時候,就要用到key,找出對應的value) of any additional information
category Gives additional information about the action to execute.
type Specifies an explicit type (a MIME type) of the intent data.






class MyListener implements OnClickListener{
public void onClick(View v){
Intent intent = new Intent();//建立一個Intent物件
Intent.setClass(Activity_1.this,Activity_2.class);//設定該intent要使用的裝置,由第二個引數返回
Activity_1.this.startActivity(intent);//事件的響應
}
}






Intent1.putExtra(“label”,”Yelnosh”);
Intent intent = getIntent();
String value = intent.getStringExtra("lable");
Intent也可用於activity與其他應用程式之間的通訊,比如下面的例子:
Uri uri = Uri.parse("smsto:18707192345");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body","Yelbosh I love you!");
this.startActivity(intent);
這樣我們就不用在新建一個acitivity了,而是直接由本次的activity跳轉到發簡訊的介面,而且直接按傳送便可以了

相關文章