gpt4 book ai didi

android - 从浏览器启动 Android 应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:06 25 4
gpt4 key购买 nike

我查看了几个与我的标题相似的 Stack Overflow 问题。每个人都得到了回答,原作者似乎很满意,但当我试图复制他们的结果时,我却空手而归。显然我做错了一些可怕的事情,但我似乎无法弄清楚。

作为引用,这些是我一直在研究的问题:

最终,我想在用户执行 OAuth 身份验证请求后使用它重新启动我的应用程序。但是,我的真实应用程序有点太大,无法在此处发布。对于这个问题,我整理了一个简单的应用程序,向您展示我所做的。我有两个 Activity 。这主要是因为我见过的每个示例都使用了使用 VIEW 操作的 Activity 。我更喜欢使用 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>

主.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!");
}
}

}

回调 Activity .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 我会得到不同的结果,具体取决于它是如何完成的。如果我在网址栏中输入它,我会在谷歌上搜索 ex1://helloworld。如果我通过重定向执行此操作,则会收到“网页不可用”。如果我尝试使用类似于以下内容的方式直接启动 Intent :

    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 只能从浏览器或 WebViewVIEW

此外,您的Intent 过滤器都不包含BROWSABLE 类别,因此它们永远无法在浏览器中使用。

不建议发明自己的方案。对于您控制的某些域,您可以使用常规 HTTP 方案。 This sample project证明了这一点。特别是,您可以遵循此过滤器显示的模式:

<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 查看条码扫描器应用程序使用的这项技术。 .

关于android - 从浏览器启动 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6139508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com