1

我在 android 中制作 webview,我是 android 的初学者。在我的代码下方,我面临以下问题。

  1. 后退按钮不起作用,当我访问我的应用程序的不同页面并按下后退按钮时,它不起作用并给出错误应用程序已停止并在我的手机主屏幕上收回我。

  2. 第二,当任何用户打开此应用程序时,如果互联网未连接或无法正常工作,那么它会发出警报,例如“此应用程序需要互联网连接”,并且默认情况下在后台我给定的背景显示 wihotu 互联网连接。如果找到互联网连接,则此应用程序可以正常工作。

我是安卓新手。在此我非常感谢您的帮助。并请在您的回答中编写代码以解决以下问题。提前致谢

package com.example.edarabia;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.annotation.SuppressLint;
import android.view.KeyEvent;


@SuppressLint("SetJavaScriptEnabled")

public class MainActivity extends Activity {
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView mywebview = (WebView) findViewById(R.id.webview);
        mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
    WebSettings webSettings = mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setBuiltInZoomControls(true);
    mywebview.setWebViewClient(new WebViewClient());
}



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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}



}
4

4 回答 4

0

您可以执行以下操作,

1.覆盖后退按钮功能

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

2.为了在应用程序启动时检查互联网连接,请将以下代码放入 onCreate()

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null) {
            if (!info.isConnected()) {

                Toast.makeText(this,
                        "Please check your wireless connection and try again.",
                        Toast.LENGTH_LONG).show();

            }

            // if positive, fetch the articles in background
            else
                // Do your task...
        } else {
            Toast.makeText(this,
                    "Please check your wireless connection and try again.",
                    Toast.LENGTH_SHORT).show();
        }
于 2013-01-16T12:42:07.350 回答
0

您还没有初始化 mainWebView webView;并且您在您的onBackPressed()方法中引用了它,这就是它给您错误的原因。

只需在您的文件中进行如下更改:

public class MainActivity extends Activity {
    WebView mywebview;   <<--- instead of webview use mywebview which you have declared in your onCreate.
   @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     mywebview = (WebView) findViewById(R.id.webView1);
     mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
     WebSettings webSettings = mywebview.getSettings();
     webSettings.setJavaScriptEnabled(true);
     webSettings.setBuiltInZoomControls(true);
     mywebview.setWebViewClient(new WebViewClient());
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.activity_main, menu);
     return true;
 }
 @Override
 public void onBackPressed (){
 if(mywebview.canGoBack()) mywebview.goBack();
    else super.onBackPressed();  
 }
 }

对于您的第二个查询:

您可以使用以下方法检查您的互联网连接:

 public static boolean IsNetConnected()
{
    boolean m_netConnected = false;
    try
    {
        ConnectivityManager m_connectivity = (ConnectivityManager) AppConstants.m_context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (m_connectivity == null)
        {
            Log.w(TAG, "couldn't get connectivity manager");
            m_netConnected = false;
        }
        else
        {
            NetworkInfo[] m_info = m_connectivity.getAllNetworkInfo();
            if (m_info != null)
            {
                for (int i = 0; i < m_info.length; i++)
                {
                    if (m_info[i].getState() == NetworkInfo.State.CONNECTED)
                    {
                        m_netConnected = true;
                    }
                }
            }
        }
    }
    catch (Throwable e)
    {
        ErrorReporter.getInstance().handleException(e);
        if (AppConstants.DEBUG)
        {
            Log.e(TAG, "IsNetConnected Error" + e.toString(), e);
            }
        m_netConnected = false;
    }
    return m_netConnected;
}

您可以使用上述方法,如下所示:

  if(!IsNetConnected())
    {
   showDialog(MainActivity.this,"This application required internet connection");
     //set your default background as per your requirements.
   }
于 2013-01-16T12:44:48.627 回答
0
  1. 你应该删除

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

避免 NullPointerException。你为什么使用这样的实现?
2. 您可以将此类功能用于您的目的:

    public boolean isOnline() {
       boolean res = false;
       ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo netInfo = cm.getActiveNetworkInfo();
       if (netInfo != null && netInfo.isConnectedOrConnecting()) {
           res= true;
       }
       else
       {
           Toast.makeText(context, "You've lost internet connection. Please check it and try again", Toast.LENGTH_LONG).show();
           res = false;
        }
       return res;
   }

并在活动中:

boolean isOnline = isOnline();
if (isOnline)
 {
      //What you want to do if there's a connection..
  }
于 2013-01-16T12:40:08.420 回答
0

覆盖onBackPressed()的功能

@Override
  public void onBackPressed() {
      finish();
  }

希望这会帮助你。

于 2017-09-20T13:26:12.003 回答