0

所以我有一个基于 webview 的应用程序。我的问题是,当OnReceivedError互联网丢失时被调用,我想

当用户单击对话框上的“确定”以检查连接时,

  • 如果连接可用,则解除警报并致电web.reload();
  • 如果没有网络连接(wifi 或手机),请致电finish();

通过以下方式解决:

  • 将警报对话框 Postive 按钮更改为以下内容

    .setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() {         
     public void onClick(DialogInterface dialog,int id) {
    
  • 问题出在以下

    DialogInterface paramDialogInterface,int paramInt)
    

上下文上下文;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  this.context = this;
  ...
  @Override
public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(
                Webview_Main.this);
        localBuilder2.setTitle(R.string.webview_error_received_title);
        localBuilder2.setMessage(R.string.webview_error_received);
        localBuilder2.setIcon(R.drawable.ic_launcher);
        localBuilder2.setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                 if(isNetworkAvailible(context))
                 web.reload();
                 else
                 finish();
         }
    };
        localBuilder2.show();
    };

isNetworkAvailible 类

   public boolean isNetworkAvailible(Context ctx) {

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();

    if (info == null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return false;
    }
    return true;
}
4

4 回答 4

2

由于代码中的关键字引用 DialogInterface.OnClickListener,因此未为该类型定义 getSystemService,因此您必须从引用当前活动的上下文变量调用 getSystemService,例如,如下所示:

context.getSystemService(Activity.CONNECTIVITY_SERVICE)
于 2013-04-19T22:43:27.063 回答
1

更正我的答案:

    Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            this.context = this; 
    }


@Override
public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(
                Webview_Main.this);
        localBuilder2.setTitle(R.string.webview_error_received_title);
        localBuilder2.setMessage(R.string.webview_error_received);
        localBuilder2.setIcon(R.drawable.ic_launcher);
        localBuilder2.setPositiveButton(R.string.alert_dialog_ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface paramDialogInterface,int paramInt) {
                    if(haveInternet(context))
                        web.reload();
                    else
                        finish();
                }
            });
        localBuilder2.show();
    };

public boolean haveInternet(Context ctx) {

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return false;
    }
    return true;
}
于 2013-04-19T22:46:36.903 回答
0

我可能会错过很多时间,但看起来你正在创建一个DialogInterface.OnClickListener,但没有分配它来处理任何事情。

更改您的代码:

    localBuilder2.setPositiveButton(R.string.alert_dialog_ok, null);

    localBuilder2.setOnClickListener(new DialogInterface.OnClickListener() {
        ...
    });
于 2013-04-19T22:52:39.527 回答
0

检查网络可用性

在您的活动中

if(CheckNetwork.isInternetAvailable(ActvityName.this))// pass activity context
     {
             //do soemthing
     }  

如果您需要检查非活动类中的网络可用性,则需要将活动上下文作为参数传递给类构造函数并使用活动上下文。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    MyAnotherClass = new MyAnotherClass(MainActivtity.this); 

}

在你的 MyAnotherClass

    Class MyAnotherClass
      {
          Context context;
         public MyAnotherClass(Context activitycontext)
         {
             this.context= activitycontext;
             if(CheckNetwork.isInternetAvailable(context))
             {
                     //do something
             }
         }
      } 

CheckNetwork 类

public class CheckNetwork {


private static final String TAG = CheckNetwork.class.getSimpleName();



public static boolean isInternetAvailable(Context context)
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null)
    {
         Log.d(TAG,"no internet connection");
         return false;
    }
    else
    {
        if(info.isConnected())
        {
            Log.d(TAG," internet connection available...");
            return true;
        }
        else
        {
            Log.d(TAG," internet connection");
            return true;
        }

    }
}
}
于 2013-04-20T04:23:54.030 回答