Intent的使用:
Intent是Android的灵魂,通过Intent可以实现很多Activity之间用于传递参数的方法。
1.使用Intent来调用Android内置的拨号功能以及发短讯的功能:
设计一个Button用于点击后来拨打电话(可以参看之前Button的学习)
public class MainActivity extends Activity {
//定义一个Button
private Button mainButton = null;
private static final int REQUEST_CODE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过ID获取Button的布局
mainButton = (Button)findViewById(R.id.mainBut);
//定义Button的点击事件
mainButton.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//声明一个Intent,用来调用系统的拨号程序
Intent intent = new Intent();
//
// //Intent的动作是打电话
// intent.setAction(Intent.ACTION_CALL);
//
// //Intent的动作是发短信
// intent.setAction(Intent.ACTION_SENDTO);
//
// //定义打电话的号码是123456
// intent.setData(Uri.parse("tel:123456"));
//
// //发送短信的号码是5554
// intent.setData(Uri.parse("smsto:5554"));
//
// intent.putExtra("sms_body", "Hello World");
//
//向SecondActivity中传递一个参数
intent.setClass(MainActivity.this, SecondActivity.class);
//传递的参数名称为"str",内容为"Intent Demo"
intent.putExtra("str", "Hello World");
//用来接收SecondActivity传递过来的参数
startActivityForResult(intent, REQUEST_CODE);
//开始动作
startActivity(intent);
}
};
@Override
//这一步是用来就收SecondActivity传递过来的参数。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQUEST_CODE){
if(resultCode==SecondActivity.RESULT_CODE){
Bundle bundle = data.getExtras();
String str = bundle.getString("back");
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show
();
}
}
}
}
****这里要强调的是,完成上面的代码后,Android虚拟机还是不能够正确执行,当你点击Call按钮时会报错
,那是因为没有注册权限,要在AndroidManifest.xml中声明你的打电话和发短信的权限:
电话权限是: <uses-permission android:name="android.permission.CALL_PHONE"/>
短信权限是:<uses-permission android:name="android.permission.SEND_SMS"/>
2.重要的是Intent之间用于Activity传递参数,在实际开发中会很有帮助:
建立两个Activity:MainActivity.java已经建立了,现在创建一个SecondActivity。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView text;
private Button secondButton;
public static final int RESULT_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
//返回一个Bundle对象
Bundle bundle = intent.getExtras();
//通过Bundle来接受名称为“str”的字符串
String str = bundle.getString("str");
text = (TextView)findViewById(R.id.secondText);
//将“str”中的内容显示出来
text.setText(str);
secondButton = (Button)findViewById(R.id.secondBut);
secondButton.setOnClickListener(listenr);
}
private OnClickListener listenr = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("back", "Second Data");
setResult(RESULT_CODE, intent);
finish();
}
};
}
****需要注意的是,但你新建了一个Activity时,直接运行是找不到你新建的Activity的,需要在
AndroidManifest.xml中注册你新建的Activity的,学过java web的朋友会更有了解,类似于web.xml的功能。
<activity
android:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
同时还要新建一个SecondActivity的布局文件,second.xml放在layout文件的目录下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/secondText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/secondBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SecondButton"
/>
</LinearLayout>
main.xml的配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/mainBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call"
/>
</LinearLayout>
这样就可以完成一个简单的Intent的实例:使用Intent进行拨号,发短信,Acticity之间传递参数,接受参数
Intent是Android的灵魂,通过Intent可以实现很多Activity之间用于传递参数的方法。
1.使用Intent来调用Android内置的拨号功能以及发短讯的功能:
设计一个Button用于点击后来拨打电话(可以参看之前Button的学习)
public class MainActivity extends Activity {
//定义一个Button
private Button mainButton = null;
private static final int REQUEST_CODE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过ID获取Button的布局
mainButton = (Button)findViewById(R.id.mainBut);
//定义Button的点击事件
mainButton.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
//声明一个Intent,用来调用系统的拨号程序
Intent intent = new Intent();
//
// //Intent的动作是打电话
// intent.setAction(Intent.ACTION_CALL);
//
// //Intent的动作是发短信
// intent.setAction(Intent.ACTION_SENDTO);
//
// //定义打电话的号码是123456
// intent.setData(Uri.parse("tel:123456"));
//
// //发送短信的号码是5554
// intent.setData(Uri.parse("smsto:5554"));
//
// intent.putExtra("sms_body", "Hello World");
//
//向SecondActivity中传递一个参数
intent.setClass(MainActivity.this, SecondActivity.class);
//传递的参数名称为"str",内容为"Intent Demo"
intent.putExtra("str", "Hello World");
//用来接收SecondActivity传递过来的参数
startActivityForResult(intent, REQUEST_CODE);
//开始动作
startActivity(intent);
}
};
@Override
//这一步是用来就收SecondActivity传递过来的参数。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQUEST_CODE){
if(resultCode==SecondActivity.RESULT_CODE){
Bundle bundle = data.getExtras();
String str = bundle.getString("back");
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show
();
}
}
}
}
****这里要强调的是,完成上面的代码后,Android虚拟机还是不能够正确执行,当你点击Call按钮时会报错
,那是因为没有注册权限,要在AndroidManifest.xml中声明你的打电话和发短信的权限:
电话权限是: <uses-permission android:name="android.permission.CALL_PHONE"/>
短信权限是:<uses-permission android:name="android.permission.SEND_SMS"/>
2.重要的是Intent之间用于Activity传递参数,在实际开发中会很有帮助:
建立两个Activity:MainActivity.java已经建立了,现在创建一个SecondActivity。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView text;
private Button secondButton;
public static final int RESULT_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
//返回一个Bundle对象
Bundle bundle = intent.getExtras();
//通过Bundle来接受名称为“str”的字符串
String str = bundle.getString("str");
text = (TextView)findViewById(R.id.secondText);
//将“str”中的内容显示出来
text.setText(str);
secondButton = (Button)findViewById(R.id.secondBut);
secondButton.setOnClickListener(listenr);
}
private OnClickListener listenr = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("back", "Second Data");
setResult(RESULT_CODE, intent);
finish();
}
};
}
****需要注意的是,但你新建了一个Activity时,直接运行是找不到你新建的Activity的,需要在
AndroidManifest.xml中注册你新建的Activity的,学过java web的朋友会更有了解,类似于web.xml的功能。
<activity
android:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
同时还要新建一个SecondActivity的布局文件,second.xml放在layout文件的目录下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/secondText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/secondBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SecondButton"
/>
</LinearLayout>
main.xml的配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/mainBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call"
/>
</LinearLayout>
这样就可以完成一个简单的Intent的实例:使用Intent进行拨号,发短信,Acticity之间传递参数,接受参数