2014-11-5 星期三 16:43:47
![]()
![]()
说明:
本例由两个Android应用组成,其中FilterProducer中的主窗口类(FilterProducerActivity)设置了多个过滤器,FilterConsumer会通过不同的过滤条件来调用FilterProducer的主窗口
运行截图:
源代码:
1、FilterProducerActivity的manifest文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="mobile.android.filter.producer"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="mobile.android.filter.producer.FilterProducerActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><intent-filter><action android:name="android.intent.action.MYACTION1" /><action android:name="android.intent.action.MYACTION2" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.MYCATEGORY1" /><category android:name="android.intent.category.MYCATEGORY2" /></intent-filter><intent-filter><action android:name="android.intent.action.MYACTION3" /><action android:name="android.intent.action.MYACTION4" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.MYCATEGORY3" /><category android:name="android.intent.category.MYCATEGORY4" /></intent-filter><intent-filter><action android:name="android.intent.action.MYACTION5" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.MYCATEGORY5" /><data android:scheme="http" /><data android:mimeType="audio/*" /><data android:mimeType="video/*" /></intent-filter><intent-filter><action android:name="android.intent.action.MYACTION6" /><category android:name="android.intent.category.DEFAULT" /><dataandroid:host="*"android:mimeType="audio/*"android:path="/work/upload.jsp"android:port="8080"android:scheme="http" /></intent-filter><intent-filter><action android:name="android.intent.action.MYACTION7" /><category android:name="android.intent.category.DEFAULT" /><dataandroid:host="192.168.17.100"android:mimeType="audio/*"android:port="8080"android:scheme="ftp" /><data android:pathPattern=".*.html" /></intent-filter><intent-filter><action android:name="android.intent.action.MYACTION7" /><category android:name="android.intent.category.DEFAULT" /><dataandroid:host="192.168.17.111"android:mimeType="audio/*"android:path="/work/test.up"android:pathPattern=".*.html"android:pathPrefix="/test"android:port="8888"android:scheme="https" /></intent-filter></activity></application></manifest>
2、/FilterConsumer/src/mobile/android/filter/consumer/FilterConsumerActivity.java
package mobile.android.filter.consumer;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;public class FilterConsumerActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_filter_consumer);}public void onClick_IntentFilter2(View view){//在这里使用了Action和两个category(使用一个也行),未使用data,通过验证Intent intent = new Intent("android.intent.action.MYACTION1");intent.addCategory("android.intent.category.MYCATEGORY1");intent.addCategory("android.intent.category.MYCATEGORY2");startActivity(intent);}public void onClick_IntentFilter3(View view){//只用Action验证,通过验证//其实系统默认为Intent对象添加了android.intent.category.DEFAULTIntent intent = new Intent("android.intent.action.MYACTION4");startActivity(intent);}public void onClick_IntentFilter4(View view){//Uri匹配了scheme,并匹配了第一个data,通过验证Intent intent = new Intent("android.intent.action.MYACTION5");intent.setDataAndType(Uri.parse("http://www.microsoft.com:8888/index.html"), "audio/mp3");startActivity(intent);}public void onClick_IntentFilter5(View view){//通过验证Intent intent = new Intent("android.intent.action.MYACTION6");intent.setDataAndType(Uri.parse("http://www.google.com:8080/work/upload.jsp"), "audio/*");startActivity(intent);}public void onClick_IntentFilter6(View view){//通过验证Intent intent = new Intent("android.intent.action.MYACTION7");intent.setDataAndType(Uri.parse("ftp://192.168.17.100:8080/work/upload.html"), "audio/*");startActivity(intent);}public void onClick_IntentFilter7(View view){//三条Uri都可以通过验证Intent intent = new Intent("android.intent.action.MYACTION7");//intent.setDataAndType(Uri.parse("https://192.168.17.111:8888/work/test.up"), "audio/wav");//intent.setDataAndType(Uri.parse("https://192.168.17.111:8888/test/up.aspx"), "audio/wav");intent.setDataAndType(Uri.parse("https://192.168.17.111:8888/p/m/abc.html"), "audio/wav");startActivity(intent);}}
3、/FilterConsumer/res/layout/activity_filter_consumer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"android:orientation="vertical"><Button android:layout_width="match_parent"android:layout_height="wrap_content" android:onClick="onClick_IntentFilter2"android:text="使用第2个Intent Filter" /><Button android:layout_width="match_parent"android:layout_height="wrap_content" android:onClick="onClick_IntentFilter3"android:text="使用第3个Intent Filter" /><Button android:layout_width="match_parent"android:layout_height="wrap_content" android:onClick="onClick_IntentFilter4"android:text="使用第4个Intent Filter" /><Button android:layout_width="match_parent"android:layout_height="wrap_content" android:onClick="onClick_IntentFilter5"android:text="使用第5个Intent Filter" /><Button android:layout_width="match_parent"android:layout_height="wrap_content" android:onClick="onClick_IntentFilter6"android:text="使用第6个Intent Filter" /><Button android:layout_width="match_parent"android:layout_height="wrap_content" android:onClick="onClick_IntentFilter7"android:text="使用第7个Intent Filter" /></LinearLayout>
本文详细介绍了Android应用间的通信方式,重点讲解了IntentFilter的配置及使用,包括如何通过设置Action和Category来实现不同应用间的交互,以及如何通过Data和MimeType来匹配特定的URI或文件类型。

被折叠的 条评论
为什么被折叠?



