一、Intent分为两类:
(1)显示Intent,通过名字指定目标组件。显式的intent用于程序内部消息——如:Activity启动一个下属服务或启动一个姊妹Activity。
(2)隐式的Intent,没有指定目标,Android系统需要查找最适合处理intent的组件(或几个组件)——一个单一的Activity。通过把intent对象的内容和intent管理器比较,判断那个组件是潜在的接收者。过滤器提供组件的能力并且划定它可以处理的intent。它开启可以接收隐式intent的组件类型。如果组件没有intent过滤器,它仅仅可以接收显式的intent。含有过滤器的组件既可以接收隐式intent也可以接收显式intent。
二、传递数据
(1) 通过Bundle打包批量传数据
Bundle bundle=new Bundle();
bundle.putString("name", name);
bundle.putInt("age", 16);
intent.putExtras(bundle)
(2)使用intent.putExtra("key", value)一个个传输数据。
package com.example.sayhellotoyou;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText etName;
private static final String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName=(EditText)findViewById(R.id.etName);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sayHello(View view){
String name = etName.getText().toString();
Intent intent =new Intent();
/*intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel://5556"));*/
/*intent.setClass(this, ShowActivity.class);*/<pre class="html" name="code">package com.example.sayhellotoyou;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ShowActivity extends Activity {
private TextView tvName;
private String name;
private int age;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
getData();
initViews();
bindData();
}
private void bindData() {
// TODO Auto-generated method stub
tvName.setText("名字是"+name+"年龄是"+age+"。");
}
private void initViews() {
// TODO Auto-generated method stub
tvName =(TextView)findViewById(R.id.tvName);
}
private void getData() {
// TODO Auto-generated method stub
/*Bundle bundle=getIntent().getExtras();
name=bundle.getString("name");
age=bundle.getInt("age");*/
name=getIntent().getStringExtra("name");
age=getIntent().getIntExtra("age",12);
}
}