返回顶部

收藏

Android 隐性Intent的例子

更多

Android的Intent分为两大类,显性的(Explicit)的和隐性的(Implicit)。显性的很简单就是我们常用的Activit跳转,他指明了从一个Activity跳转到另一个。在SecondActivity这个配置中有一个intent-filter,其中它定义了action的名称。所谓的隐性Intent就是靠这个action的名称来传递。代码如下:

显式

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

Main.java

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);
}
}

SecondActivity.java

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);
}
}
}

[XML]代码

<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>

标签:Android,java

收藏

0人收藏

支持

0

反对

0

发表评论