如何从我的应用程序中打开活动的网址中检索变量“?var = value”?

时间:2013-01-25 10:46:48

标签: android url android-intent android-manifest

我按照我找到here的示例来获取网络链接以打开我的应用活动之一。我得到了它的工作,但现在我需要让它检索url所具有的变量值对。

当我点击此网址http://www.tapabook.com/tapa/?tapa=578时,它会打开正确的活动,但getPathSegments()仅检索1个细分,即“tapa”(而不是“tapa = 578”)。有没有办法找回整对?

这是我的清单,如果它有一些问题,它会阻止我想要的东西

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alberovalley.tappabook"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock.Light.DarkActionBar" 
    android:name="com.alberovalley.tappabook.Tappabook">

    <activity
        android:name="com.alberovalley.tappabook.actividad.LoginA"
        android:configChanges="orientation"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name="com.alberovalley.tappabook.actividad.MenuFA" 
        android:label="@string/app_name">
    </activity>
    <activity
        android:name="com.alberovalley.tappabook.actividad.ListaTapasFA"            
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.alberovalley.tappabook.actividad.DetalleTapaFA"
        android:label="@string/app_name" >
        <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:host="tapabook.com"
               android:pathPrefix="/tapa/"
               android:scheme="http" />
           <data
               android:host="www.tapabook.com"
               android:pathPrefix="/tapa/"
               android:scheme="http" />
        </intent-filter>
    </activity>

</application>

</manifest>

这是我的活动中的代码,我检查是否从Intent.ACTION_VIEW调用它,并在上述情况下检索数据。

   Intent i = getIntent();
    final String action = i.getAction();
    Log.d(CData.LOGTAG, "acción del intent " + action);
    if (Intent.ACTION_VIEW.equals(action)) {
        final List<String> segments = i.getData().getPathSegments();
        Log.d(CData.LOGTAG, "Contenido nº segmentos URL interceptada " + segments.size());
        if (segments.size() > 0) {
            Log.d(CData.LOGTAG, "Contenido de la URL interceptada [" + segments.get(0) + "]");
            idTapa = Long.parseLong( segments.get(0) );
        }
    }else{
        idTapa = i.getExtras().getLong(TapaDao.ID, -1);
        Log.d(CData.LOGTAG, "DetalleTapaFA.onCreate idTapa = " + idTapa);
    }

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

getEncodedQuery()将为您提供Uri的整个查询部分。来自文档:

  

从此URI获取编码的查询组件。查询后来了   查询分隔符('?')和片段分隔符('#')之前。   这个方法会返回“q = android”   “http://www.google.com/search?q=android”。

使用它代替getPathSegments()