开发者

How can we view webpages in our android application without opening browser?

Hey i am developing an android application , and i want to connect to web inside that application. However i have tried WebView to someextent but its displaying file's on my directory fine but when connecting to google.com it display's an error !

Then i added this file <uses-p开发者_Go百科ermission android:name="android.permission.INTERNET" />

in my Manifest.xml and now the the url(google.com) is being displayed in the browser Any help how i can open the browser inside my application ?


Using webview.

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewDemo extends Activity 
{
    private WebView mWebView = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com/");
    }
}

add <uses-permission android:name="android.permission.INTERNET" /> in manifest file.

main.xml file which used here.

<?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"
    >
<WebView  
        android:id="@+id/webView"
        android:scrollbars="none" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</LinearLayout>


Another way to do this is via Intents:

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

You need to specify permission in manifest file

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


it must useful to u.. it will not load in browser..

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class WebViewDemo extends Activity {
    private class MyWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return true;
      }
  }
    private WebView webView;
    private EditText urlField;

    private Button goButton;

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

        // Create reference to UI elements
        webView  = (WebView) findViewById(R.id.webview_compontent);
        urlField = (EditText)findViewById(R.id.url);
        goButton = (Button)findViewById(R.id.go_button);

        // workaround so that the default browser doesn't take over
        webView.setWebViewClient(new MyWebViewClient());

        // Setup click listener
        goButton.setOnClickListener( new OnClickListener() {
            public void onClick(View view) {
                openURL();
            }
        });

        // Setup key listener
        urlField.setOnKeyListener( new OnKeyListener() {
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_ENTER) {
                    openURL();
                    return true;
                } else {
                    return false;
                }
            }
        });

    }

    /** Opens the URL in a browser */
    private void openURL() {
        webView.loadUrl(urlField.getText().toString());
        webView.requestFocus();
    }    
}

xml file

<?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"
    >

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

        <EditText  
        android:id="@+id/url" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"     
        android:lines="1"
        android:layout_weight="1.0" android:hint="http://"/>

        <Button
        android:id="@+id/go_button"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"     
        android:text="@string/go_button"
        />

    </LinearLayout>

    <WebView  
        android:id="@+id/webview_compontent"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="1.0"   
    />
</LinearLayout>

and in mainfiest file don't forget to give internet permission.. i hope this help u..


You can use webview.What you said,it doesn't show google.Maybe problem is in other place.There is no way of being an error while showing google.Webview is ofcourse for the purpose of showing web pages.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜