1

我是android开发的初学者。下面我给出我的主要activity.java代码。我的要求,如果互联网连接或 wifi 不能在移动设备上工作,然后执行以下操作

  1. 如果互联网连接不起作用,则在应用程序启动时显示我的初始屏幕背景,并提供对话框或警报,如旅行顾问的此屏幕截图检查此图像和屏幕截图。如果取消按钮按下然后对话框消失,只显示启动画面,如果按下再试一次,然后尝试执行连接到互联网。

在我的代码中,我使用代码购买对话框,我不想要购买功能,这不是我的要求。我想要和旅行顾问一模一样。对我来说很容易,如果有人编辑我的代码并显示在哪里进行更改,非常感谢提前

package com.example.edarabia;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;


@SuppressLint("SetJavaScriptEnabled")

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






//  @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);

//  }

 // 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) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

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


public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url);
        return true;
    }
}

//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
    ConnectivityManager connectivity = (ConnectivityManager)     getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
    }
    return false;
}

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("just for testing");

    builder.setMessage("Check you net conectivity....");
    builder.setCancelable(false);
    builder.setPositiveButton("Buy", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent browserIntent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://www.google.com"));
            startActivity(browserIntent);
        }
    });
    builder.show();
}




}
4

3 回答 3

1

这可能会有所帮助: 检测 Android 上是否有可用的 Internet 连接

或尝试深入研究广播接收器。示例代码:

public class InternetConnectionStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        _v("Network connectivity change");
        if (intent.getExtras() != null) {
            NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
                _v("Network " + ni.getTypeName() + " connected");
                onNetworkConnection(context, true);
            }
        }
        if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
            _v("There's no network connectivity");
            onNetworkConnection(context, false);
        }
    }

    private void onNetworkConnection(Context context, boolean isConnected) {
          //show dialog
    }

}
于 2013-01-17T14:43:14.140 回答
0

我得到了解决方案,并在更新的代码下方

package com.example.edarabia;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;


@SuppressLint("SetJavaScriptEnabled")

public class MainActivity extends Activity{
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(isNetworkConnected() == true){
    setContentView(R.layout.activity_main);
    mywebview = (WebView) findViewById(R.id.webview);       
    mywebview.getSettings().setJavaScriptEnabled(true);
    mywebview.setWebViewClient(new myWebClient());
    mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
    mywebview.getSettings().setBuiltInZoomControls(true);
    mywebview.getSettings().setLoadWithOverviewMode(false);
    mywebview.getSettings().setUseWideViewPort(false);
    }else{
    setContentView(R.layout.splash);    
    showBuyDialog();
    }
}






//  @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);

//  }

 // 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) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

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


public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url);
        return true;
    }
}

//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
    }
    return false;
}

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Unable to connect");

    builder.setMessage("You Must have an Internet connection to use Edarabia. Please connect and try again.");
    builder.setCancelable(false);
    builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            finish(); 
        }
    });
    builder.show();
}




}
于 2013-01-18T09:20:46.320 回答
0

尝试替换 showButDialog() 方法,

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Unable to connect");   
    builder.setMessage("You must have an Internet connection to use TripAdvisor. Please connect and try again.");
    builder.setCancelable(false);

    builder.setPositiveButton("Try again", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int id) {
               isNetworkConnected();
         }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.show();
}

为了获得与图片中相同的 UI,请在application标记前添加以下行,如下所示,

<uses-sdk android:targetSdkVersion="15" />

Android OS 3.0 (Honey Comb) 及更高版本可使用此类对话框。所以你在以前的版本中看不到对话框。

于 2013-01-21T06:22:24.013 回答