复习:
尽量使用效率高的Parcelable传递对象。
隐式意图:没有明确指定组件名的Intent为隐式意图。 Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(URI和数据类型)找到最合适的组件来处理这个意图。
怎么使用隐式意图
①在Mainfest.xml的Activity里配置
设置一个过滤器
<intent-filter >
<action android:name="com.huaao.OtherActivity"/>//要匹配对应的动作
<category android:name="android.intent.category.DEFAULT"/>//可以引用一个默认的类型,也可以自定义
<data android:scheme="hoo"/>//协议部分,要访问的话,前面也要加协议
<data android:host="www.hoo.com"/>
<data android:path="/person"/>
<data android:mimeType="image/gif" />" />//数据类型
《mime Type表》http://blog.csdn.net/u013032932/article/details/40829007
</intent-filter>
设置一个动作
intent.setAction("com.huaao.OtherActivity");
设置一个数据
intent.setData(Uri.parse("hoo://www.hoo.com/person"));//setData表示传递值
设置一个类型,会消除前面设置的数据
intent.setType("image/gif");
设置数据和类型
intent.setDataAndType(Uri.parse("hoo://www.hoo.com/person"), "image/gif");
上课案例 :跨应用程序的调用
简介:先安装应用程序(一),再安装(二)。在应用程序(二)中点击按钮后,跳转到(一),并且获得数据。
应用程序(一)
AndroidManifest.xml
<!-- 配置Activity的实例 -->
<activity android:name=".otherActivity">
<intent-filter >
<action android:name="com.ryan.action.other"/>
<category android:name="android.intent.category.DEFAULT" />
<data
android:scheme="http"
android:host="www.baidu.com"
android:path="/index.jsp"
android:mimeType="image/png"/>
</intent-filter>
</activity>
otherActivity.java
public class otherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Intent intent = getIntent();
Uri uri = intent.getData();//获得数据
String host = uri.getHost();//得到主机名
String scheme = uri.getScheme();//得到协议
String path = uri.getPath();//得到path部分
System.out.println(host);
System.out.println(scheme);
System.out.println(path);
}
}
应用程序(二)
MainActivity.java
public class MainActivity extends Activity {
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.ryan.action.other");//设置动作
intent.setDataAndType(Uri.parse("http://www.baidu.com/index.jsp"), "image/png");//设置数据和类型
startActivity(intent);//启动隐式意图的Activity
}
});
}
}
Tips:设置数据和类型不能分开用setData 和setType