【发布时间】:2011-05-26 13:44:58
【问题描述】:
我查看了几个标题与我类似的 Stack Overflow 问题。每个都有答案,原作者似乎很满意,但是当我试图复制他们的结果时,我空手而归。显然我做错了什么,但我似乎无法弄清楚。
作为参考,这些是我一直在研究的问题:
- 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
最终,一旦用户执行了 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!”。我不确定我做错了什么。有什么见解吗?
【问题讨论】: