问题描述
|
我看了几个与我的标题相似的Stack Overflow问题。每个答案都有,原始作者似乎很满意,但是当我尝试复制他们的结果时,我空手而归。显然,我正在做一些可怕的错误,但是我似乎无法弄清楚。
作为参考,以下是我一直在研究的问题:
从Android浏览器启动自定义Android应用程序
在Android浏览器中建立链接启动我的应用程序
Android Dev回调网址无法正常工作
Android意图过滤器:如何正确使用可浏览的意图类别
最终,一旦用户执行了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,我将根据其完成方式获得不同的结果。如果我在网址栏中输入它,则会在Google搜索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!\”。我不确定自己做错了什么。有什么见解吗?
解决方法
您不能使用
MAIN
-Android中的URL仅从浏览器中ѭ7或WebView
。
另外,您的Intent
过滤器都不包含BROWSABLE
类别,因此它们将永远无法与浏览器一起使用。
不建议发明自己的方案。您可以对您控制的某些域使用常规的HTTP方案。此示例项目演示了这一点。特别是,您可以遵循此过滤器显示的模式:
<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应用程序使用的这项技术。
,如果要从浏览器启动应用程序,请尝试此操作
我昨天做了
1)在localhost上创建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)在模拟器上安装应用程序后,打开浏览器并转到localhost
点击链接和您的应用程序,如果已安装将启动,否则将是错误的