|
Android的Intent分为两大类,显性的(Explicit )的和隐性的(Implicit)。
显性的很简单就是我们常用的Activit跳转,他指明了从一个Activity跳转到另一个,代码如下
Intent i = new Intent(this,AnotherActivity.class);
startActivity(i);
最多加一些需要传递的数据,或者回调时的参量这时需要用startActivityForResult()
隐性的没有指明从哪跳转到哪,需要自定义Action。
首先是第一个Activity,它定义了mapSearchIntent这个Action
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);
}
}
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);
}
}
然后是要跳转的Activity:
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);
}
}
}
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);
}
}
} |
|