2016-06-20 53 views
0

我的應用的用戶可以使用父項和子項創建自定義列表。我將這些信息存儲在一個xml文件中。我想給用戶 分享這個文件(電子郵件等)的機會。如果其他用戶點擊此文件,我想打開該應用程序並在我的列表中顯示數據!Android分享並打開XML文件

到目前爲止我所做的: 用戶可以創建一個xml文件並將其保存在他的存儲中。 現在的問題是:如何在文件上單擊時打開應用程序?我是否必須創建自己的具有XML數據的文件擴展名?

編輯:

<activity 
      android:name=".activities.TrainingSimpleShowActivity" 
      android:label="@string/title_activity_training_simple_show" 
      android:screenOrientation="portrait"> 

      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data android:scheme="file" 
        android:host="*" 
        android:pathPattern=".*\\.xml" 
        android:mimeType="*/*" /> 
      </intent-filter> 

     </activity> 

回答

1

沒有,你只需要設置你的應用的意圖過濾器。

例如:

<intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <data android:scheme="file" android:host="*" android:pathPattern=".*\\.xml" android:mimeType="*/*" /> 
</intent-filter> 

所以,當用戶點擊到XML文件,打開你的應用程序可以建議從Android系統打開此文件。

然後,您必須在應用程序中獲取Intent並處理數據。

Intent intent = getIntent(); 
Uri data = intent.getData(); 

也許你可以寫一個指導爲你寫的用戶這個XML文件,而當您的應用的意圖得到的日期,你可以搜索特定的標籤或屬性。

希望這對你有所幫助。

UPDATE 的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.dojester13.experimentintent"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <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" /> 
      <data android:scheme="file" android:host="*" android:pathPattern=".*\\.xml" android:mimeType="*/*" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 
+0

我在哪裏必須把意圖過濾在清單? – XxGoliathusxX

+0

在manifest.xml中,在您想要處理數據的活動中,主要是我認爲在這種情況下。我建議你檢查文檔https://developer.android.com/guide/components/intents-filters.html –

+0

好吧,我把它添加到我想要處理它的活動。但是當我點擊xml文件並說「打開...」時,它不會在列表中顯示我的應用程序。 – XxGoliathusxX