如何创建一个打开网站的Android应用程序

时间:2014-11-18 11:58:37

标签: java android eclipse

我正在使用eclipse。刚开始使用android web apps :)!

我想创建一个Web应用程序,无论何时打开它,它都会加载一个网站。

没有视觉,没有按钮。

这是我的主要空白代码:

package com.example.yellowtest;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

4 个答案:

答案 0 :(得分:3)

此处为Web应用程序的示例代码

activity_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/google"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</RelativeLayout>  

主要课程

public class MainActivity extends Activity {
  private WebView webView;

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

    webView = (WebView) findViewById(R.id.google);
    startWebView("https://www.google.com/");
  }


  @SuppressLint("SetJavaScriptEnabled")
  private void startWebView(String url) {
    webView.setWebViewClient(new WebViewClient() {      
      ProgressDialog progressDialog;
        public boolean shouldOverrideUrlLoading(WebView view, String url) {              
          view.loadUrl(url);
          return true;
        }

        public void onLoadResource (WebView view, String url) {
          if (progressDialog == null) {
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.show();
          }
        }

        public void onPageFinished(WebView view, String url) {
          try {
            if (progressDialog.isShowing()) {
              progressDialog.dismiss();
              progressDialog = null;
             }
           } catch(Exception exception) {
             exception.printStackTrace();
           }
        }

      }); 

      webView.getSettings().setJavaScriptEnabled(true); 
      webView.getSettings().setLoadWithOverviewMode(true);
      webView.getSettings().setUseWideViewPort(true);
      webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
      webView.setScrollbarFadingEnabled(false);
      webView.getSettings().setBuiltInZoomControls(true);
      webView.loadUrl(url);    
    }

    @Override
    public void onBackPressed() {
      if(webView.canGoBack()) {
        webView.goBack();
      } else {
        super.onBackPressed();
      }
    }
}

在AndroidManifest.xml中使用此权限

 <uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:0)

public class MainActivity extends ActionBarActivity {

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

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

答案 2 :(得分:0)

查看带有API的Web应用的PhoneGap / Cordova http://cordova.apache.org/

答案 3 :(得分:0)

使用以下代码。它包含最安全的webview客户端。

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/*
 * Demo of creating an application to open any URL inside the application and clicking on any link from that URl 
should not open Native browser but  that URL should open in the same screen.
 */
public class WebViewClientDemoActivity extends Activity {
    /** Called when the activity is first created. */

    WebView web;

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

        web = (WebView) findViewById(R.id.webview01);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://www.google.com");
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}