【问题标题】:Redirect to a webpage while opening an android app打开安卓应用程序时重定向到网页
【发布时间】:2015-11-27 13:04:01
【问题描述】:

我需要开发一个应用程序,在应用程序启动后立即将用户重定向到我的网站。

这就是我想要的移动应用程序。

我正在使用 Android 工作室,但我不熟悉 XML。所以我被困在那里。我应该编写哪个代码来执行此重定向?希望大家能帮帮我。谢谢。

【问题讨论】:

  • 看看this question的答案
  • 所以不要显示启动画面,而是添加一个网页视图

标签: java android xml android-studio mobile-application


【解决方案1】:

您可以在 XML 文件中使用 Web 视图并在其中加载您的 url

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.com");

【讨论】:

  • 在 xml 中使用 schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
  • 我已在您的答案中添加了代码,但它不起作用,没有任何反应。所以我的总代码是这样的
  • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.loadUrl("http://www.google.com"); </RelativeLayout>
  • 在你的 xml 文件中添加这个
  • 并在您的活动中添加代码 WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.loadUrl("google.com");
【解决方案2】:

首先你需要导入这个:

import android.content.Intent; 
import android.net.Uri;

在您的 MainActivity.java 类中,您可以添加 OnCreate 方法

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent i = new Intent(
            Intent.ACTION_VIEW,
            Uri.parse("http://www.google.com")
        );

// Starts Implicit Activity
        startActivity(i);
    }

【讨论】:

  • 这段代码给了我一些错误:( "Error:(16, 9) error: cannot find symbol class Intent"
  • 你必须导入 Intent 类,请在 MainActivity 类上方添加。导入android.content.Intent;导入android.net.Uri;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 2016-09-07
  • 1970-01-01
  • 2017-10-12
  • 2017-07-28
  • 1970-01-01
相关资源
最近更新 更多