Android开发--Intent的用法小结

1、显式Intent的使用

Activity中的onCreate方法中增加该代码:

//使用Intent显式跳转
button1=(Button)findViewById(R.id.fa_button1);
button1.setOnClickListener(new OnClickListener() {
	public void onClick(View arg0) {
		Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
		startActivity(intent);
	}
});

 

2、隐式Intent的使用

Activity中的onCreate方法中增加该代码:

//使用Intent隐式跳转
button2=(Button)findViewById(R.id.fa_button2);
button2.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent("com.ljy.activity.ACTION_START");
	intent.addCategory("com.ljy.activity.MY_CATEGORY");
		startActivity(intent);
	}
});

在AndroidManifest.xml文件中需要跳转目标Activity代码中增加下列代码:

<activity android:name="com.ljy.activity.SecondActivity">
    <intent-filter>
    	<action android:name="com.ljy.activity.ACTION_START"/>
    	<category android:name="android.intent.category.DEFAULT"/>
    	<category android:name="com.ljy.activity.MY_CATEGORY"/>
    </intent-filter>
</activity>

 

3、更多隐式Intent的用法----调用系统浏览器展示网页

Activity中的onCreate方法中添加下列代码:

//使用Intent调用系统浏览器展示网页
button3=(Button)findViewById(R.id.fa_button3);
button3.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setData(Uri.parse("http://www.baidu.com"));
		startActivity(intent);
	}
});

 

4、使用Intent调用系统拨号界面
在Activity中的onCreate方法中添加下列代码:

//使用Intent调用系统拨号界面
button4=(Button)findViewById(R.id.fa_button4);
button4.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent(Intent.ACTION_DIAL);
		intent.setData(Uri.parse("tel:10086"));
		startActivity(intent);
	}
});

 

5、使用Intent向下一个活动传递数据
在Activity中的onCreate方法中添加下列代码

//使用Intent传递数据到SecondActivity
button5=(Button)findViewById(R.id.fa_button5);
button5.setOnClickListener(new OnClickListener(){
	@Override
	public void onClick(View arg0) {
		String data = "Hello SecondActivity";
		Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
		intent.putExtra("extra_data", data);
		startActivity(intent);
	}
});

在SecondActivity中的onCreate方法中添加下列代码取出Intent传递的数据

//取出FristActivity传递过来的数据
Intent intent = getIntent();
String data = intent.getStringExtra("extra_data");
Toast.makeText(this, "取出的数据是:" + data, Toast.LENGTH_LONG).show();

 

6、使用Intent接收活动返回的数据
在Activity中的onCreate方法中添加下列代码:

//使用Intent接收SecondActivity返回的数据
button6=(Button)findViewById(R.id.fa_button6);
button6.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View arg0) {
		String data = "这是Button6的数据";
		Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
		intent.putExtra("extra_data", data);
		startActivityForResult(intent, 1);
	}
});

在SecondActivity中的onCreate方法中添加下列代码使用Intent返回数据给FirstActivity

//使用Intent返回数据给FirstActivity
return_data=(Button)findViewById(R.id.sa_button2);
return_data.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent();
		intent.putExtra("data_return", "Hello FirstActivity");
		setResult(RESULT_OK, intent);
		finish();
	}
});

在FristActivity中重写onActivityResult()方法,代码如下:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	switch (requestCode) {
	case 1:
		if(resultCode == RESULT_OK) {
		String returnedData = data.getStringExtra("data_return");
		Toast.makeText(FirstActivity.this, "返回的数据是" + returnedData, Toast.LENGTH_LONG).show();
		}
		break;

	default:
		break;
	}
}

对SecondActivity的返回键进行监听,修改onBackPressed()方法,代码如下:

@Override
public void onBackPressed() {
	Intent intent = new Intent();
	intent.putExtra("data_return", "Hello FirstActivity");
	setResult(RESULT_OK, intent);
	finish();
}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值