使用Intent Android获取网址

时间:2015-02-09 22:00:27

标签: android android-intent

我试图获取点击的网址以打开应用。我只是无法找到可以获取该信息的位置。我有一个清单,点击后打开应用程序。链接将是" 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();
        }
}

3 个答案:

答案 0 :(得分:5)

您应该能够使用以下内容获取活动中的意图数据:

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

答案 1 :(得分:0)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: database
  labels:
    app: database
spec:
  replicas: 1
  selector:
    matchLabels:
      app: database
  template:
    metadata:
      labels:
        app: database
    spec:
      containers:
        - image: mariadb
          name: database
          env:
            - name: MYSQL_DATABASE
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_DATABASE
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_PASSWORD
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_ROOT_PASSWORD
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_USER
          ports:
            - containerPort: 3306
              name: database
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: database-claim0

您将不得不尝试/抓住周围。

答案 2 :(得分:0)

基本上,一旦您获得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. 假设用户点击了网址“ 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”。

希望有帮助!