打开浏览器到网页的Android应用程序

5

我正在尝试掌握Java和Android,希望在单击按钮后打开用户浏览器的简单任务中获得帮助。

我已经做了两天的教程,但如果我自己尝试并得到反馈可能会更有帮助。提前感谢任何帮助。

main.xml:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bgimage2"> 
    >

<Button
android:id="@+id/goButton"
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/start"
android:layout_x="80px"
android:layout_y="21px"
>
</AbsoluteLayout>

GetURL.java:

package com.patriotsar;

import android.app.Activity;
import android.content.Intent;
import android.view.View.OnClickListener;

String url = "http://www.yahoo.com";
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(url);
i.setData(u);


public class patriosar extends Activity {

     private Button goButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        goButton.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v){
                try {
                      // Start the activity
                      startActivity(i);
                    } catch (ActivityNotFoundException e) {
                      // Raise on activity not found
                      Toast toast = Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
                    }
                  } 
        });

    }
}

2
那么...问题是什么?代码能正常工作吗?代码失败了吗?会发生什么? - trutheality
1
另外,摆脱AbsoluteLayout。该容器类已被弃用。使用其他东西。 - CommonsWare
@trutheality 不,代码将在模拟器中运行。我会确保在下一篇文章中发布我的错误信息。 - Denoteone
@CommonsWare 我之前使用了另一个布局,但在教程中看到了这个。感谢提醒。 - Denoteone
有没有办法只使用Javascript来实现这个功能? window.open('http://myurl.com','Name')无效,它只是重新打开了应用程序。 - Wytze
2个回答

6

这段代码基本正确,但是有一些地方位置不对或者缺少了。下面的代码可以正常工作--我做了最小必要修改。你可以将两个版本加载到 WinMerge 等软件中,以查看确切的更改内容。

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"
    android:background="@drawable/bgimage2"
    >

    <Button
        android:id="@+id/goButton"
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/start"
        android:layout_x="80px"
        android:layout_y="21px"
    ></Button>
</LinearLayout>

GetURL.java:

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class GetURL extends Activity {
    private Button goButton;
    String url = "http://www.yahoo.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    Uri u = Uri.parse(url);
    Context context = this;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        goButton = (Button)findViewById(R.id.goButton);
        goButton.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v){
                try {
                      // Start the activity
                        i.setData(u);
                      startActivity(i);
                    } catch (ActivityNotFoundException e) {
                      // Raise on activity not found
                      Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
                    }
                  } 
        });
    }
}

当然,你还需要在/res/drawable/中拥有一个bgimage2.png文件,在/res/values/strings.xml中也需要有一个start字符串。


谢谢你抽出时间来帮助我,我会比较两者并找出我所缺失的。 - Denoteone
啊,谢谢——我看到那个问题之外的部分时只是稍微忽略了一下。 - Sven Viking

4

为了简化操作,您可以这样做:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com")); startActivity(intent);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接