intent 和intent Filters
startActivity()的机制
用到了IBinder ipc 用到了进程间通讯机制
activity有四种LaunchMode
当startActivity()的时候不知道启动的是不是和自己的activity在一个
进程中,所以要用 IPC 进程间通讯来调用
简单的用法
1
A.class中
1
2
3
|
Intent intent =
new
Intent(A.
this
, B.
class
);
intent.putExtra(
"sundy.demo"
,
"你好"
);
startActivity(intent);
|
B.class中
1
2
3
|
Intent intent =
this
.getIntent();
String
value = intent.getExtras().getString(
"key"
);
Toast.makeText(
this
, value,
1
).show();
|
2
A.class中
1
2
3
4
|
Intent intent =
new
Intent();
intent.putExtra(
"key"
,
"123"
);
intent.setAction(
"com.wang.cn"
);
startActivity(intent);
|
B。class中
.
1
2
3
|
Intent intent =
this
.getIntent();
String
value = intent.getExtras().getString(
"key"
);
Toast.makeText(
this
, value,
1
).show();
|
要在mainfest中设置B。clas的activity中的intent-filter的action中设置
1
2
3
4
5
6
7
|
<activity android:name=
".B"
>
<intent-filter>
<action android:name=
"com.wang.cn"
/>
<category android:name=
"android.intent.category.DEFAULT"
/>
</intent-filter>
</activity>
|
必须写上 <category android:name="android.intent.category.DEFAULT" />这一句不然会报错。。
3. 简单的打电话 代码
1
2
3
4
5
|
Intent intent =
new
Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse(
"tel:12345645555"
));
startActivity(intent);
|
setAction和setData都是系統定義好 。這裡只說下用法
4.获取data中的值
A。class中
1
2
3
4
|
Intent intent =
new
Intent();
intent.setAction(
"com.wang.cn"
);
intent.setData(Uri.parse(
"tel:12345645555"
));
startActivity(intent);
|
B。class中
1
2
3
4
|
Intent intent =
this
.getIntent();
String
uri = intent.getDataString();
Toast.makeText(
this
, uri,
1
).show();
|
setAction和setData都是系統定義好 。這裡只說下用法
1
2
3
4
5
6
7
8
9
10
|
<activity android:name=
".Rose"
>
<intent-filter>
<action android:name=
"com.wang.cn"
/>
<category android:name=
"android.intent.cat egory.DEFAULT"
/>
<data android:scheme=
"tel"
>
</data>
</intent-filter>
</activity>
|
5.startActivityForResult 方法
A。class中
1
2
3
|
Intent intent =
new
Intent();
intent.setClass(A.
this
,B.
class
);
startActivityForResult(intent,
123
);
|
在A。clas的activity中 导入系统的onActivityResult方法
1
2
3
4
5
6
7
8
9
|
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
// TODO Auto-generated method stub
super
.onActivityResult(requestCode, resultCode, data);
if
(resultCode ==
321
) {
String
value = data.getExtras().getString(
"name"
);
Toast.makeText(
this
, value,
1
).show();
}
}
|
B.class中
1
2
3
4
5
6
7
8
9
10
11
|
button.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View arg0) {
Intent intent =
this
.getIntent();
intent.putExtra(
"name"
,
"111111111"
);
setResult(
321
, intent);
finish();
}
});
|
当resultCode一样的时候 回传值成功。。
6.intent 传递 对象 类 等等