2014-10-02 57 views
1

如果应用程序安装在设备上,我想在链接点击而不是浏览器上打开我的应用程序,否则打开Goog​​le play。 所以搜索后..这是我的清单在链接点击而不是浏览器上打开我的应用程序 - Android

<activity 
     android:name="example.com.MyActivity" 
     android:label="@string/title_activity_open_event_details" > 
     <intent-filter> 
      <data 
       android:host="play.google.com" 
       android:scheme="http"/> 
      <action android:name="android.intent.action.VIEW" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 


     </intent-filter> 
    </activity> 

,这是我的活动

public class MyActivity extends Activity { 

private int EventId; 
private TextView tv; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_open_event_details); 

    tv = (TextView) findViewById(R.id.testtv); 

    Uri data = getIntent().getData(); 
    if (data != null) 
    { 
     String scheme = data.getScheme(); 
     String host = data.getHost(); 
     List<String> params = data.getPathSegments(); 
     String first = params.get(0); 
     String second = params.get(1); 

     int size = params.size(); 
    // EventId = params.get(size-1); 
     String third = params.get(size-1); 
     tv.setText(data.toString()); 
    } 


    } 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.my_activity, menu); 
    return true; 
} 

} 

,当我这个链接https://play.google.com/store/apps/details?id=example.com&evid=117 MyActivity成功打开,但显示的链接上点击的TextView只https://play.google.com/store/apps/details?id=example.com没有& evid = 117我需要获取一些数据并将其显示在我的活动中

请问您能否帮助我了解我在想什么G ? 在此先感谢

+0

如果用户选择浏览器为默认应用,会发生什么打开网址? – Salmaan 2015-02-10 04:54:50

回答

1

要获得ID试试这个代码:

Uri data = getIntent().getData(); 
    if (data != null) { 
     String url = data.toString(); 
      try { 
       List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8"); 
       for (NameValuePair para : params) 
        if (para.getName().equals("evid")) { 
         String id = para.getValue(); //my id (117) 
         Log.v("myId",id); 
        } 
      } catch (URISyntaxException e) { 
       e.printStackTrace(); 
      } 

     } 

而且你不包括https协议。添加

<data android:host="play.google.com" android:scheme="https"/> 

编辑

我已经测试此代码,它的工作。我创建了一个简单的活动与我上面提供的代码和意图过滤器:

<intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
<intent-filter> 
     <category android:name="android.intent.category.DEFAULT"/> 
     <category android:name="android.intent.category.BROWSABLE"/> 
     <action android:name="android.intent.action.VIEW"/> 
     <data android:host="play.google.com" android:scheme="http"/> 
     <data android:host="play.google.com" android:scheme="https"/> 
</intent-filter> 

输出结果是,正如所料,117

+0

感谢您的回答..但我的问题是我在活动中收到的url不包含“&evid = 117”部分..当我调试时,我发现url只是[link] https:// play。 google.com/store/apps/details?id=example.com 我不知道为什么网址应该是[link] https://play.google.com/store/apps/details?id=example。 com&evid = 117 – 2014-10-02 08:54:13

+0

您是否试过该代码?你是否也尝试在我的示例中打印'url'来查看完整的url? – 2014-10-02 08:56:12

+0

是的,我有..和网址是一样的 – 2014-10-02 08:58:33

相关问题