供参考,这些是我一直在查看的问题:
- 从Android浏览器启动自定义Android应用程序
- 让Android浏览器中的链接启动我的应用程序
- Android Dev回调URL不起作用
- Android Intent Filters:如何正确使用Intent Category Browsable
最终,我想在用户执行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!”。我不确定我的操作有什么问题。有什么见解吗?
http
方案而不是 PDF MIME 类型的东西。如果您查看 LogCat,您将看到类似于以下内容:I/ActivityManager(1177): START u0 {act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=http://commonsware.com/Android/excerpt.pdf cmp=android/com.android.internal.app.ResolverActivity (has extras)}
。如果您在选择器中选择 Web 浏览器,则会启动下载,然后显示一个带有 URLHandler 的选择器,以查看已下载的文件。 - CommonsWare<intent-filter>
元素进行设计,即使会导致一些元素的重复。 - CommonsWare