我已经看过与我的类似标题的几个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