Android 隐性Intent的例子

Android的Intent分为两大类,显性的(Explicit )的和隐性的(Implicit)。 
显性的很简单就是我们常用的Activit跳转,他指明了从一个Activity跳转到另一个。 

在SecondActivity这个配置中有一个intent-filter,其中它定义了action的名称。所谓的隐性Intent就是靠这个action的名称来传递。 
代码如下:
标签: Android SDK  Intent

[1].[代码] 显式 跳至 [1] [2] [3] [4]

?
1
2
Intent i = new Intent( this ,AnotherActivity. class );
startActivity(i);

[2].[代码] Main.java 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
 
public class Main extends Activity {
private final String mapSearchIntent = "com.decarta.mapsearch.intent.action.SEARCH" ;
 
/** Called when the activity is first created. */
 
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri mapUri = Uri.parse( "geo:39.906033,116.397700" );
Intent i = new Intent(mapSearchIntent, mapUri);
i.setData(mapUri);
startActivity(i);
}
}

[3].[代码] SecondActivity.java 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
 
/**
* @author Tony Shen
*/
 
public class SecondActivity extends Activity{
private Uri data;
private String action;
 
/** Called when the activity is first created. */
 
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getAction() != null )
action = intent.getAction();
if (intent.getData()!= null )
data = intent.getData();
if (action.equals( "com.decarta.mapsearch.intent.action.SEARCH" )) {
Intent i = new Intent(Intent.ACTION_VIEW, data);
startActivity(i);
}
}
}

[4].[代码] [XML]代码 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
< application android:icon = "@drawable/icon" android:label = "@string/app_name" >
< activity android:name = ".Main" android:label = "@string/app_name" >
< intent-filter >
< action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity android:name = ".SecondActivity" >
< intent-filter >
< action android:name = "com.decarta.mapsearch.intent.action.SEARCH" />
< category android:name = "android.intent.category.DEFAULT" />
< data android:scheme = "geo" />
</ intent-filter >
</ activity >
</ application >
< application android:icon = "@drawable/icon" android:label = "@string/app_name" >
< activity android:name = ".Main" android:label = "@string/app_name" >
< intent-filter >
< action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
 
< activity android:name = ".SecondActivity" >
< intent-filter >
< action android:name = "com.decarta.mapsearch.intent.action.SEARCH" />
< category android:name = "android.intent.category.DEFAULT" />
< data android:scheme = "geo" />
</ intent-filter >
</ activity >
</ application >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值