Intent 介绍:
Intent中文翻译为 “意图”,它是Android程序中各个组件进行交互的一种重要方式,它不仅可以指定当前组件要执行的 动作,还可以在不同组件之间进行 数据传递。
先来开两张效果图片:
下面就是代码部分:
首先在AndroidManifest.xml中设置一下权限(在application外设置):
<!-- 设置打电话权限 -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- 设置发短信权限 -->
<uses-permission android:name="android.permission.SEND_SMS" />
使用intent调用打电话功能:
先在中activity_main.xml新建一个按钮
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拨打电话"/>
MainActivity.java类(点击事件可以直接实例化,不用在下面重新创建方法,我是因为有几个方法写在一起的):
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过id找到button
button = (Button) findViewById(R.id.button);
//绑定点击事件
button.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//创建Intent对象
Intent intent = new Intent();
switch (v.getId()) {
case R.id.button:
//设置操作为打电话,setAction调用设备资源
intent.setAction(Intent.ACTION_CALL);
//设置电话号码(因为类型是uri,所以要强转,电话号码前要加tel:)
intent.setData(Uri.parse("tel:110"));
//执行方法
startActivity(intent);
break;
default:
break;
}
}
};
使用intent调用发短信功能:
先在中activity_main.xml新建一个按钮
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发短信"/>
MainActivity.java类(点击事件可以直接实例化,不用在下面重新创建方法,我是因为有几个方法写在一起的):
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过id找到button
button1 = (Button) findViewById(R.id.button1);
//绑定点击事件
button1.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//创建Intent对象
Intent intent = new Intent();
switch (v.getId()) {
case R.id.button1:
//设置操作为发短信,setAction调用设备资源
intent.setAction(Intent.ACTION_SENDTO);
//设置电话号码,发给谁(因为类型是uri,所以要强转电话号前要加smsto)
intent.setData(Uri.parse("smsto:10086"));
//短信内容
intent.putExtra("sms_body", "查话费");
//执行方法
startActivity(intent);
break;
default:
break;
}
}
};
使用intent实现页面跳转其他网址(这里跳转到百度):
先在中activity_main.xml新建一个按钮:
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转百度页面"/>
MainActivity.java类(点击事件可以直接实例化,不用在下面重新创建方法,我是因为有几个方法写在一起的):
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过id找到button
button2 = (Button) findViewById(R.id.button2);
//绑定点击事件
button2.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//创建Intent对象
Intent intent = new Intent();
switch (v.getId()) {
case R.id.button2:
//打开指定网页
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
break;
default:
break;
}
}
};
使用intent实现页面跳转并传递参数(需要新建一个页面,这里新建的页面为activity_second.xml):
activity_main.xml新建一个按钮和文本框(传值):
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="文本框"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转页面并传递值"/>
MainActivity.java类(点击事件可以直接实例化,不用在下面重新创建方法,我是因为有几个方法写在一起的):
private Button button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过id找到button
button4 = (Button) findViewById(R.id.button4);
//绑定点击事件
button4.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//创建Intent对象
Intent intent = new Intent();
switch (v.getId()) {
case R.id.button4:
//从MainActivity页面跳转到SecondActivity页面
intent.setClass(MainActivity.this, SecondActivity.class);
//将参数放入intent,和map差不多用法
intent.putExtra("parm", edit1.getText().toString());
//执行方法
startActivity(intent);
break;
default:
break;
}
}
};
SecondActivity页面(传递过来的值放在文本框中):
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" />
SecondActivity.java类:
private TextView text;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
text = (TextView) findViewById(R.id.text);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//返回上一个页面
finish();
}
});
//取得两个页面之间传递的intent
Intent intent = getIntent();
//取出绑定传递的参数
Bundle bundle = intent.getExtras();
if(bundle != null){
//取出传递参数键为parm的值(可以传递多个)
String str = bundle.getString("parm");
//把传递过来的值放入文本框中
text.setText(str);
}
}
使用intent实现页面跳转并带参数返回(需要新建一个页面,这里新建的页面为activity_three.xml):
activity_main.xml新建一个按钮:
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带参数带返回值"/>
MainActivity.java类(点击事件可以直接实例化,不用在下面重新创建方法,我是因为有几个方法写在一起的):
private static final Integer REQUEST_CODE = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//查看返回过来的参数
if(requestCode == REQUEST_CODE && resultCode == ThreeActivity.RESULT_LONG){
Bundle bundle = data.getExtras();
String str = bundle.getString("aaa");
//弹框显示传递过来的参数
Toast.makeText(MainActivity.this, "返回参数:"+str, Toast.LENGTH_LONG).show();
}
}
private Button button5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过id找到button
button5 = (Button) findViewById(R.id.button5);
//绑定点击事件
button5.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//创建Intent对象
Intent intent = new Intent();
switch (v.getId()) {
case R.id.button5:
intent.setClass(MainActivity.this, ThreeActivity.class);
//启动Activity并返回结果
startActivityForResult(intent, REQUEST_CODE);
break;
default:
break;
}
}
};
activity_three.xml文件(返回按钮和一个返回的文本框):
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="返回值" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回并传递值" />
ThreeActivity.java类:
private EditText edit;
private Button button;
public static final Integer RESULT_LONG = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
edit = (EditText) findViewById(R.id.edit);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("aaa", edit.getText().toString());
setResult(RESULT_LONG, intent);
finish();
}
});
}
Intent还有很多功能,这里就写这么多了(主要常用的也就这些),其他的就不一一列举了!