赞
踩
Intent(意图)主要是解决Android应用的各项组件之间的通讯
Intent又分为两种,一种是显示意图一种是隐式意图.
显示意图:
这个意图非常简单,传递两个参数即可,比如如下代码
//第一个参数为当前的activity,第二个参数则是要跳转的activity.
Intent
it
=
new
Intent
(
MyActivity
.
this
,
OtherActivity
.
class
);
startActivity(it);
//这个参数就是action
Intent it = new Intent("android.test.MyActivity.ACTION_TEST");
//如果category是default的,那么此行代码则可以注释
it.addCategory("android.intent.category.TEST");
startActivity(it);
<activity android:name="com.test.demo1.Demo2" android:theme="@style/AppBaseTheme" android:label="@string/app_name">
<intent-filter>
<action android:name="android.test.MyActivity.ACTION_TEST"/>
<category android:name="android.intent.category.TEST"/>
</intent-filter>
</activity>
Intent it = new Intent(Intent.ACTION_VIEW);
it.setData(Uri.parse("http://www.php0.net"));
startActivity(it);
Intent it = new Intent(Demo1.this,Demo2.class);
it.putExtra("id", 12312312);
startActivity(it);
Intent it = getIntent();
int id = it.getIntExtra("id",0);
Intent it = new Intent(Demo1.this,Demo2.class);
startActivityForResult(it,1); //第二个参数是个唯一值就行
Intent it = new Intent(Demo2.this,Demo1.class);
it.putExtra("is_read", "已阅");
setResult(RESULT_OK,it);
finish();
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case 1:
if(resultCode == RESULT_OK){
String is_read = data.getStringExtra("is_read");
Toast.makeText(getApplicationContext(), is_read, 1).show();
}
break;
default:
}
}
@Override
public void onBackPressed() {
Intent it = new Intent();
it.putExtra("is_read", "已阅");
setResult(RESULT_OK,it);
finish();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。