Android学习之Intent使用
2017-09-13 16:50
302 查看
1、使用显示Intent
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
上述代码的作用是打开活动SecondActivity
2、使用隐式Intent
首先打开AndroidManifest.xml,添加代码:
<activity
android:name="com.example.activitytest.SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY" />
</intent-filter>
</activity>
然后修改FirstActivity中按钮的点击事件:
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);
}
});
还可以使用隐式Intent,启动其他程序的活动。
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.cnblogs.com/zhouhb/"));
//Intent intent=new Intent(Intent.ACTION_DIAL);
//intent.setData(Uri.parse("tel:12345"));
3、使用Intent在Activity之间传递数据
3.1 从FirstActivity向SecondActivity传递数据
String s="from first";
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("data", s);
startActivityForResult(intent, 1);
3.2 SecondActivity接收传递来的数据
Button btn=(Button)findViewById(R.id.button2);
Intent intent=getIntent();
String string=intent.getStringExtra("data");
btn.setText(string);
3.3 SecondActivity向FirstActivity返回数据
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
returnData();
}
});
private void returnData() {
Intent intent=new Intent();
intent.putExtra("returnData", "from Second");
setResult(RESULT_OK,intent);
finish();
}
//如果用户不是通过点击按钮,而是按下Back键回到FirstActivity,则重写 onBackPressed
public void onBackPressed() {
// TODO Auto-generated method stub
returnData();
}
3.4 在FirstActivity中重写onActivityResult得到SecondActivity返回的数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case 1:
if(resultCode==RESULT_OK){
String string=data.getStringExtra("returnData");
Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
上述代码的作用是打开活动SecondActivity
2、使用隐式Intent
首先打开AndroidManifest.xml,添加代码:
<activity
android:name="com.example.activitytest.SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY" />
</intent-filter>
</activity>
然后修改FirstActivity中按钮的点击事件:
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);
}
});
还可以使用隐式Intent,启动其他程序的活动。
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.cnblogs.com/zhouhb/"));
//Intent intent=new Intent(Intent.ACTION_DIAL);
//intent.setData(Uri.parse("tel:12345"));
3、使用Intent在Activity之间传递数据
3.1 从FirstActivity向SecondActivity传递数据
String s="from first";
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("data", s);
startActivityForResult(intent, 1);
3.2 SecondActivity接收传递来的数据
Button btn=(Button)findViewById(R.id.button2);
Intent intent=getIntent();
String string=intent.getStringExtra("data");
btn.setText(string);
3.3 SecondActivity向FirstActivity返回数据
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
returnData();
}
});
private void returnData() {
Intent intent=new Intent();
intent.putExtra("returnData", "from Second");
setResult(RESULT_OK,intent);
finish();
}
//如果用户不是通过点击按钮,而是按下Back键回到FirstActivity,则重写 onBackPressed
public void onBackPressed() {
// TODO Auto-generated method stub
returnData();
}
3.4 在FirstActivity中重写onActivityResult得到SecondActivity返回的数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case 1:
if(resultCode==RESULT_OK){
String string=data.getStringExtra("returnData");
Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
相关文章推荐
- android学习记录(五)显式Intent与隐式Intent的功能与使用方法解析。
- Android学习13--使用Intent和IntentFilter进行通信
- Android(java)学习笔记146:Bundle和Intent类使用和交互
- Android Studio 学习 - 基本控件的使用;Intent初学
- 学习Android中Intent的使用
- Android学习之Intent使用
- Android学习备忘011——使用Intent传递数据之返回结果
- Android学习总结(十)———— Intent的使用
- android学习:通过Intent打开Activity的两种方法,Intent过滤器的使用方法
- android学习之使用Intent
- Android学习之Intent使用
- android学习之路-使用Intent跳转
- Android学习——Intent的使用
- Android学习之Intent过滤器的使用
- Android开发之基本组件学习-----------Intent的使用
- Android Intent的几种用法全面总结(供自我学习查阅使用)
- android学习4#--使用Intent创建Activity
- Android学习之 Intent几种使用方法使用
- [Android--Code]Intent的学习与使用
- Android初级学习之Intent的简单使用