【问题标题】:Launching an Android Application from the Browser从浏览器启动 Android 应用程序
【发布时间】:2011-05-26 13:44:58
【问题描述】:

我查看了几个标题与我类似的 Stack Overflow 问题。每个都有答案,原作者似乎很满意,但是当我试图复制他们的结果时,我空手而归。显然我做错了什么,但我似乎无法弄清楚。

作为参考,这些是我一直在研究的问题:

最终,一旦用户执行了 OAuth 身份验证请求,我想使用它来重新启动我的应用程序。但是,我的实际应用程序有点太大,无法在此处发布。对于这个问题,我整理了一个简单的应用程序,向您展示我所做的事情。我有两个活动。这主要是因为我看到的每个示例都使用使用 VIEW 操作的活动。我更喜欢使用 MAIN 动作,但我在这里都使用了这两种方式来表明我都尝试过。

AndroidManifest.xml(原始;请参阅下面的编辑版本)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".HelloAndroidActivity">
        <intent-filter>
            <data android:scheme="ex1"/>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".CallbackActivity">
        <intent-filter>
            <data android:scheme="ex2"/>
            <action android:name="android.intent.action.VIEW"/>
        </intent-filter>
    </activity>
</application>

AndroidManifest.xml(根据评论编辑)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".HelloAndroidActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".CallbackActivity">
        <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="www.this-so-does-not-exist.com" android:scheme="http" />
        </intent-filter>
    </activity>
</application>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

HelloAndroidActivity.java

package org.example;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroidActivity extends Activity {

    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textView);
    }

    @Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data != null) {
            textView.setText("Hello Web!");
        }
    }

}

CallbackActivity.java

package org.example;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class CallbackActivity extends Activity {

    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textView);
    }

    @Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data != null) {
            textView.setText("Hello Web!");
        }
    }

}

这构建得很愉快。然后,在浏览器中,如果我输入 ex1://helloworld 或 ex2://helloworld,我会得到不同的结果,具体取决于它是如何完成的。如果我在 url 栏中输入它,我会在谷歌上搜索 ex1://helloworld。如果我通过重定向执行此操作,我会得到“网页不可用”。如果我尝试使用类似于以下内容的内容直接启动意图:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    ComponentName componentName = new ComponentName(
            "org.example",
            "org.example.HelloAndroidActivity");
    intent.setComponent(componentName);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setData(Uri.parse("ex1://helloworld"));
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    startActivity(intent);

我得到默认的“Hello my-example!”。我不确定我做错了什么。有什么见解吗?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    您不能使用MAIN -- Android 中的 URL 只能来自浏览器的 VIEWed 或 WebView

    此外,您的Intent 过滤器都不包含BROWSABLE 类别,因此它们永远无法与浏览器一起使用。

    不建议自己发明方案。对于您控制的某些域,您可以使用常规 HTTP 方案。 This sample project 证明了这一点。特别是,您可以遵循此过滤器显示的模式:

    <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="www.this-so-does-not-exist.com" android:scheme="http" />
    </intent-filter>
    

    用您拥有的东西替换www.this-so-does-not-exist.com,并可能添加path 以缩小过滤器的范围。您还可以从ZXing 看到 Barcode Scanner 应用使用的这种技术。

    【讨论】:

    • 我已经提出了你建议的建议,但我似乎仍然无法让它发挥作用。当我在地址栏中输入this-so-does-not-exist.com 时,我得到:“网页不可用”。
    • @Tim:我不知道它是否可以从地址栏工作。关键是它可以通过链接工作。
    • 那么问题就来了。感谢您的帮助!
    • 我刚刚检查了ZXing app。它不像建议的那样工作。在这种情况下你能帮我吗...
    • @NiravShah:如果您在使用 ZXing 应用时遇到问题,请联系 ZXing 团队。
    【解决方案2】:

    试试这个,如果你想从浏览器启动你的应用程序 我昨天做了这个

    1) 在 localhost 上制作 html 页面

    <html>
    <head>
    </head>
    <body>
    <a href="yourownscheme://example.com/">Test link</a>
    </body>
    </html>
    

    2) 创建一个类

       import org.xml.sax.Parser;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;
    
    public class URLHandler extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
    
        TextView uri=(TextView)findViewById(R.id.uri);
        TextView uri2=(TextView)findViewById(R.id.uri);
    
    
        if (Intent.ACTION_MAIN.equals(getIntent().getAction())) {
          String intentUri=(new Intent("yourownscheme://example.com/")).toUri(Intent.URI_INTENT_SCHEME).toString();
    
    
    
          uri.setText(intentUri);
    
        }
       }
      }
    

    3) 清单如下

    <?xml version="1.0" encoding="utf-8"?>
    <manifest android:versionCode="1"
              android:versionName="1.0"
              package="yourpackage name"
              xmlns:android="http://schemas.android.com/apk/res/android">
    
      <supports-screens android:largeScreens="true"
                        android:normalScreens="true"
                        android:smallScreens="false" />
      <application android:icon="@drawable/cw"
                   android:label="@string/app_name">
    
    
    
          <activity
            android:name=".URLHandler"
            android:label="@string/app_name" 
            android:exported="true">
            <intent-filter>
    
                <data  android:scheme="yourownscheme://example.com/" />
    
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    
    
      </application>
    </manifest>
    

    4) 在模拟器上安装应用程序后,打开浏览器并转到 localhost 点击链接和你的应用程序,如果安装了就会启动,否则会出错

    【讨论】:

      猜你喜欢
      • 2011-06-05
      • 2012-06-20
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2016-09-11
      • 2023-03-03
      相关资源
      最近更新 更多