在Android应用中打开后台浏览器

时间:2016-05-23 07:32:42

标签: java android

package com.example.drexsprint.ardusecuv20;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void goToLeft(View view) {
        goToUrl("http://user:password@10.0.1.16/?button2");
    }

    public void goToRight(View view) {
        goToUrl("http://user:password@10.0.1.16/?button3");
    }

    public void goToTemp(View view) {
        goToUrl("http://user:password@10.0.1.16/?button1");
    }

    public void goToHum(View view) {
        goToUrl("http://user:password@10.0.1.16/button1");
    }

    public void goToPic(View view) {
        goToUrl("http://user:password@10.0.1.6/image.jpg"); // this works fine to take picture
    }

    private void goToUrl(String url) {
        Uri uriUrl = Uri.parse(url);
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
        startActivity(launchBrowser);
    }

}

-----------------------布局----------------------- ---------------------------

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.drexsprint.ardusecuv20.MainActivity">

    <Button
        android:layout_width="87dp"
        android:layout_height="wrap_content"
        android:text="Show Humidty"
        android:id="@+id/button"
        android:layout_gravity="bottom"
        android:onClick="goToHum"
        android:layout_alignTop="@+id/button4"
        android:layout_toRightOf="@+id/button4"
        android:layout_toEndOf="@+id/button4"
        android:textColor="#101dae"
        android:backgroundTint="#d87c25" />

    <Button
        android:layout_width="78dp"
        android:layout_height="wrap_content"
        android:text="Rotate Left"
        android:layout_gravity="bottom"
        android:id="@+id/button3"
        android:clickable="true"
        android:autoLink="web"
        android:cursorVisible="true"
        android:linksClickable="true"
        android:onClick="goToLeft"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/button"
        android:layout_toEndOf="@+id/button"
        android:textColor="#101dae"
        android:backgroundTint="#d87c25" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rotate Right"
        android:layout_gravity="bottom"
        android:layout_weight="0.38"
        android:id="@+id/button2"
        android:autoLink="web"
        android:clickable="true"
        android:onClick="goToRight"
        android:layout_alignTop="@+id/button3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@+id/button3"
        android:layout_toEndOf="@+id/button3"
        android:textColor="#101dae"
        android:backgroundTint="#d87c25" />

    <Button
        android:layout_width="86dp"
        android:layout_height="wrap_content"
        android:text="Show Temprt"
        android:id="@+id/button4"
        android:layout_gravity="bottom"
        android:onClick="goToTemp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#101dae"
        android:backgroundTint="#d87c25" />

    <Button
        android:layout_width="86dp"
        android:layout_height="wrap_content"
        android:text="Take a Photo"
        android:id="@+id/button5"
        android:layout_gravity="bottom"
        android:onClick="goToPic"
        android:layout_above="@+id/button"
        android:layout_toLeftOf="@+id/button2"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button"
        android:textColor="#101dae"
        android:longClickable="true"
        android:backgroundTint="#d87c25" />

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/button2"
        android:layout_alignEnd="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/button5" />
</RelativeLayout>

我正在尝试创建一个Android应用程序,现在我可以单击应用程序中的一个按钮,它可以完成我想要它做的事情,问题是它总是把我带到浏览器,我希望它能够执行后台的URL,甚至不加载浏览器但实际执行命令,任何帮助!?

The pic of my app

1 个答案:

答案 0 :(得分:0)

如果您想打开应用程序中的网址,并且不想在启动浏览器时离开应用程序,为此您可以使用WebView

public class MainActivity extends AppCompatActivity {

private WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myWebView = (WebView) findViewById(R.id.webView);
    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    });

}

public void btn1(View view) {
    myWebView.loadUrl("http://user:password@10.0.1.16/?button2");
}

public void btn2(View view) {
    myWebView.loadUrl("http://user:password@10.0.1.16/?button3");
}

public void btn3(View view) {
    myWebView.loadUrl("http://user:password@10.0.1.16/?button1");
}

}

这是main.xml

<?xml version="1.0" encoding="utf-8"?>

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView"
    android:layout_gravity="center_horizontal"
    android:layout_above="@+id/btn1" />
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">


</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="btn1"
    android:text="Link1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="Link2"
    android:onClick="btn2"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/btn1"
    android:layout_toEndOf="@+id/btn1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:text="Link3"
    android:onClick="btn3"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button"
    android:layout_toEndOf="@+id/button" />
</RelativeLayout>

你可以试试这个。