2011-05-26 53 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那將是錯誤

相關問題