我已經看過與我的類似標題的幾個Stack Overflow問題。每個人都回答了,原作者似乎很滿意,但是當我嘗試複製他們的結果時,我空手而歸。很顯然,我正在做一些可怕的錯誤,但我似乎無法弄清楚。從瀏覽器啓動Android應用程序
供參考,這些都是我一直在尋找的問題:
- Launch Custom Android Application From Android Browser
- Make a Link in the Android Browser Start Up My App
- Android Dev Callback Url Not Working
- Android Intent Filters: How Do You Use Intent Category Browsable Correctly
最後,我想用這可以在用戶執行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);
我得到默認的「你好,我-例子!」。我不確定我做錯了什麼。任何見解?
我做了你的建議建議,但我似乎無法得到這個工作。當我在地址欄中鍵入http://www.this-so-does-not-exist.com時,出現:「網頁不可用」。 – Tim 2011-05-27 12:47:13
@Tim:我不知道它是否能在地址欄中工作。重點是它從一個鏈接工作。 – CommonsWare 2011-05-27 12:48:58
以及這個問題。感謝您的幫助! – Tim 2011-05-27 15:25:49