【问题标题】:Getting url with Intent Android使用 Intent Android 获取 url
【发布时间】:2015-04-09 19:27:22
【问题描述】:

我试图获取被点击的 URL 以打开应用程序。我只是很难找到我可以得到这些信息的地方。我有一个清单,单击时会打开应用程序。该链接将是“http://host.com/file.html?param=1&param2=2”,我试图将其提供给应用程序来处理。

 <intent-filter>
           <data android:scheme="http"
                android:host="host.com" 
                android:pathPrefix="/file.html" />
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.BROWSABLE" />
          <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

编辑

@Override
  protected void onCreate(Bundle savedInstanceState) 
  {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        Uri uri = intent.getData();
        try {
            url = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    您应该能够通过以下方式获取 Activity 中的意图数据:

    Uri uri = this.getIntent().getData();
    URL url = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
    

    【讨论】:

    • 我在顶部添加了我当前的 OnCreate 方法。但似乎仍然无法正常工作。
    • 我的 onCreate 方法似乎没有被调用,据我了解,当系统调用 onCreate 时它会被调用,它会重定向到我通过覆盖它创建的 onCreate。
    【解决方案2】:

    基本上,一旦您获得了您的 Uri(即getIntent().getData()),您就可以将完整的 url 读取为uri.toString()。假设您的android:host 有效且已设置,您还可以通过调用uri.getEncodedQuery() 获取实际查询数据。

    如果需要,以下是工作示例的更多详细信息:

    1. 这是我的清单设置:
                <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="boo.acme.com"
                        android:scheme="http" />
                </intent-filter>
    
    1. 这就是我阅读 Uri 的方式:
    Uri uri = this.getIntent().getData();
    
    1. 假设用户点击 URL“http://boo.acme.com/?ll=43.455095,44.177416”,uri.toString() 产生“http://boo.acme.com/?ll=43.455095,44.177416”,uri.getEncodedQuery() 产生“ll=43.455095,44.177416”。

    希望有帮助!

    【讨论】:

      【解决方案3】:
      Uri uri = this.getIntent().getData();
          try
          {
              URL url = new URL(uri.getScheme(), uri.getHost(), uri.getPath());
          }
          catch (MalformedURLException e)
          {
      
          }      
      

      你必须用 try/catch 包围。

      【讨论】:

        猜你喜欢
        • 2014-07-22
        • 1970-01-01
        • 1970-01-01
        • 2014-12-05
        • 2012-09-21
        • 1970-01-01
        • 2019-08-14
        • 1970-01-01
        • 2011-08-21
        相关资源
        最近更新 更多