1.值由A.class传递到B.class
A.class中:
01 |
Intent intent = new Intent();
|
03 |
intent.setClass(A.this,B.class);
|
05 |
intent.putExtra("username1",username);
|
06 |
intent.putExtra("userpwd1"userpwd);
|
08 |
Bundle data = new Bundle(); |
09 |
data.putString("username1",username); |
10 |
data.putString("userpwd1",userpwd); |
11 |
intent.putExtras(data); |
14 |
this.startActivity(intent);
|
B.class中:
1 |
Intent intent = getIntent(); |
3 |
String username = intent.getStringExtra("username1");
|
4 |
String userpwd = intent.getStringExtra("userpwd1");
|
5 |
/* Bundle data = intent.getExtras(); |
6 |
String username = intent.getString("username1"); |
7 |
String userpwd = intent.getString("userpwd1"); */ |
2.除了A.class可以向B.class传值外,B.class也可以返回值
A.class中
this.startActivity(intent);
改为this.startActivityFroResult(intent,1);//1为请求码
B.class中
对传过来的intent对象赋新值
1 |
intent.putExtra("username2",username2);
|
2 |
intent.putExtra("userpwd2",userpwd2);
|
3 |
this.setResult(1,intent);
|
A.class中重写
1 |
@Override protected void onActivityResult(int requestCode,int resultCode,Intent data){
|
2 |
super.onActivityResult(requestCode,resultCode,data);
|
A.class中取出B.class中intent传过来的值
3.intent.setClass(A.this,B.class)的另一种写法
在manifest.xml中B的Activity中加入
3 |
<action android:name="com.showB">//这里可以随便写
|
5 |
<category android:name = "android.intent.category.DEFAULT">
|
那么A中就可以直接写
intent.setAction("com.showB");
来代替
intent.setClass(A.this,B.class);
这也就提示了我们利用intent-filter可以实现其他很多功能
程序中调用其它程序的Activity
1 |
Intent intent = new Intent(Intent.ACTION_MAIN);
|
2 |
ComponentName componentName = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
|
3 | intent.setComponent(componentName); |
"com.android.settings"是要打开的程序包名,"com.android.settings.WirelessSettings"是要打开的Activity。