2015-08-09 74 views
0

我在查詢問題之前查看過不同的帖子,但沒有一個能夠解決我的問題。如何使用自定義URI方案打開應用程序?

我想自定義URI方案打開應用程序:MYAPP://

我試圖intent.setData(Uri.parse("myapp://")),但它無法正常工作。

所以我錯過了什麼?

回答

0

試試這個:

intent.setData(Uri.parse("myapp://path")) 

<activity android:name=".YourActivity"> 
    <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="myapp" android:host="path" /> 
    </intent-filter> 
</activity> 
+0

我也試過在我的示例應用程序中,不工作。 – dreamcoder

0

首先,你應該在你的AndroidManifest.xml中定義正確intent-filter

<application 
     <activity 
      android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <data 
        android:host="browse" 
        android:pathPrefix="" 
        android:scheme="myapp" /> 
      </intent-filter> 
     </activity> 
</application> 
在Java代碼中處理

那麼這個意圖:

if (Intent.ACTION_VIEW.equals(getIntent().getAction())) { 
    List<String> segments = getIntent().getData().getPathSegments(); 
    String section = getIntent().getData().getHost(); 
    if (segments.size() > 1 && segments.get(0).equals("browse")) { 
     // Do something here 
    } 
} 
+0

這是我的意圖過濾器: ' – dreamcoder

+0

然後使用intent.setData(Uri.parse(「temp:// sample」)) – sider

+0

我添加了java代碼示例,所以你可以在你的java代碼中處理它@dreamcoder – moallemi

相關問題