2011-05-26 190 views
4

我已经看过与我的类似标题的几个Stack Overflow问题。每个人都回答了,原作者似乎很满意,但是当我尝试复制他们的结果时,我空手而归。很显然,我正在做一些可怕的错误,但我似乎无法弄清楚。从浏览器启动Android应用程序

供参考,这些都是我一直在寻找的问题:

最后,我想用这可以在用户执行O操作后重新启动我的应用程序身份验证请求。但是,我真正的应用程序有点太大,不能在这里发布。对于这个问题,我已经组建了一个简单的应用程序,向您展示我所做的。我有两个活动。这主要是因为我见过的每个示例都使用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); 

我得到默认的“你好,我-例子!”。我不确定我做错了什么。任何见解?

回答

8

您无法使用MAIN - Android中的网址仅为VIEW,来自浏览器或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应用程序使用的这种技术。

+0

我做了你的建议建议,但我似乎无法得到这个工作。当我在地址栏中键入http://www.this-so-does-not-exist.com时,出现:“网页不可用”。 – Tim 2011-05-27 12:47:13

+1

@Tim:我不知道它是否能在地址栏中工作。重点是它从一个链接工作。 – CommonsWare 2011-05-27 12:48:58

+0

以及这个问题。感谢您的帮助! – Tim 2011-05-27 15:25:49

0

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

1)在本地主机上制作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)当你安装了找你的仿真器,打开浏览器应用程序并进入到本地主机 自来水的链接和您的应用程序,如果它的安装会推出,othervice那将是错误