从浏览器启动Android应用程序
我已经研究了几个标题与我类似的堆栈溢出问题。每个问题都得到了回答,最初的作者似乎很满意,但当我试图复制他们的结果时,我空手而归。很明显,我做错了什么,但我似乎无法理解 作为参考,以下是我一直在研究的问题:从浏览器启动Android应用程序,android,android-intent,Android,Android Intent,我已经研究了几个标题与我类似的堆栈溢出问题。每个问题都得到了回答,最初的作者似乎很满意,但当我试图复制他们的结果时,我空手而归。很明显,我做错了什么,但我似乎无法理解 作为参考,以下是我一直在研究的问题: 最后,我希望在用户执行OAuth身份验证请求后使用它重新启动应用程序。然而,我的实际应用程序有点太大,无法在这里发布。对于这个问题,我编写了一个简单的应用程序,向您展示我所做的工作。我有两项活动。这主要是因为我看到的每个示例都使用了一个使用VIEW操作的活动。我更喜欢使用主动作,但
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!");
}
}
}
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!”。我不确定我做错了什么。有什么见解吗?您不能使用
MAIN
——Android中的URL只能从浏览器或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>
.如果要从浏览器启动应用程序,请尝试此操作
我昨天做的
1) 在localhost上创建html页面
<html>
<head>
</head>
<body>
<a href="yourownscheme://example.com/">Test link</a>
</body>
</html>
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) 在emulator上安装应用程序后,打开浏览器并转到localhost
点击链接,然后点击你的应用程序,如果它安装好了,就会启动,否则就会出错我已经提出了你的建议,但我似乎仍然无法让它正常工作。当我在地址栏中输入时,我得到:“网页不可用”。@Tim:我不知道地址栏中的内容是否有效。关键是要让它从一个链接开始工作。这就是问题所在。谢谢你的帮助!我刚刚检查了ZXing应用程序。它没有按照建议的那样工作。在这种情况下你能帮我吗…@NiravShah:如果你在ZXing应用程序上有问题,请联系ZXing团队。
<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>
<html>
<head>
</head>
<body>
<a href="yourownscheme://example.com/">Test link</a>
</body>
</html>
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);
}
}
}
<?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>