1. 在AndroidManifest.xml中定义activity intent filter:
......
<activity android:name="xxxx"
android:configChanges="orientation">
<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.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:mimeType="application/pdf" android:pathPattern=".*//.pdf"/>
</intent-filter>
</activity>
......
2. activity通过intent发送数据:
...
Intent it = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse("file:///sdcard/test.xxx");
it.setDataAndType(u, "application/mp3");
it.setClassName("xxxxx", "xxxxxxx");
startActivity(it);
...
3. activity中通过intent获取数据:
String mFileName;
......
final Intent intent = getIntent();
String fileToOpen = null;
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
final Uri uri = intent.getData();
if (uri != null) {
fileToOpen = uri.getPath();
mFileName = fileToOpen;
}
intent.setData(null);
}
其中mFileName就是我们要获取的数据, 应为"file:///sdcard/test.xxx"。