这个
获取网络信息需要在AndroidManifest.xml文件中加入相应的权限。 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
1)判断是否有网络连接 
public boolean isNetworkConnected(Context context) { 
if (context != null) { 
ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
if (mNetworkInfo != null) { 
return mNetworkInfo.isAvailable(); 


return false; 
}
2)判断WIFI网络是否可用 public boolean isWifiConnected(Context context) { 
if (context != null) { 
ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mWiFiNetworkInfo = mConnectivityManager 
.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
if (mWiFiNetworkInfo != null) { 
return mWiFiNetworkInfo.isAvailable(); 


return false; 
}
3)判断MOBILE网络是否可用
public boolean isMobileConnected(Context context) { 
if (context != null) { 
ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mMobileNetworkInfo = mConnectivityManager 
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
if (mMobileNetworkInfo != null) { 
return mMobileNetworkInfo.isAvailable(); 


return false; 
}
4)获取当前网络连接的类型信息
public static int getConnectedType(Context context) { 
if (context != null) { 
ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { 
return mNetworkInfo.getType(); 


return -1; 
}
根据你的要求用上面的方法判断连接,false的时候跳出对话框,确定然后activity.finish()。
true的时候用webview加载你的url,that's all !

解决方案 »

  1.   

    请问,false的时候跳出对话框,确定然后activity.finish()。这个怎么实现,,,谢谢!!现在以经将以上几段代码加到.java文件中不报错了,就是不知道如何没有网络的时候给出对话框 提示,摸索中,谢谢
      

  2.   

    这是java文件源码
    package comhy.example.hysjmall;import comhy.example.hysjmall.util.SystemUiHider;
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.MotionEvent;
    import android.view.View;/**
     * An example full-screen activity that shows and hides the system UI (i.e.
     * status bar and navigation/system bar) with user interaction.
     * 
     * @see SystemUiHider
     */public class FullscreenActivity extends Activity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true; /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000; /**
     * If set, will toggle the system UI visibility upon interaction. Otherwise,
     * will show the system UI visibility upon interaction.
     */
    private static final boolean TOGGLE_ON_CLICK = true; /**
     * The flags to pass to {@link SystemUiHider#getInstance}.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; /**
     * The instance of the {@link SystemUiHider} for this activity.
     */
    private SystemUiHider mSystemUiHider; @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen); final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content); // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView,
    HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
    .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
    // Cached values.
    int mControlsHeight;
    int mShortAnimTime; @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public void onVisibilityChange(boolean visible) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    // If the ViewPropertyAnimator API is available
    // (Honeycomb MR2 and later), use it to animate the
    // in-layout UI controls at the bottom of the
    // screen.
    if (mControlsHeight == 0) {
    mControlsHeight = controlsView.getHeight();
    }
    if (mShortAnimTime == 0) {
    mShortAnimTime = getResources().getInteger(
    android.R.integer.config_shortAnimTime);
    }
    controlsView
    .animate()
    .translationY(visible ? 0 : mControlsHeight)
    .setDuration(mShortAnimTime);
    } else {
    // If the ViewPropertyAnimator APIs aren't
    // available, simply show or hide the in-layout UI
    // controls.
    controlsView.setVisibility(visible ? View.VISIBLE
    : View.GONE);
    } if (visible && AUTO_HIDE) {
    // Schedule a hide().
    delayedHide(AUTO_HIDE_DELAY_MILLIS);
    }
    }
    }); // Set up the user interaction to manually show or hide the system UI.
    contentView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    if (TOGGLE_ON_CLICK) {
    mSystemUiHider.toggle();
    } else {
    mSystemUiHider.show();
    }
    }
    }); // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    findViewById(R.id.dummy_button).setOnTouchListener(
    mDelayHideTouchListener);
    } @Override
    protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState); // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
    } /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
    if (AUTO_HIDE) {
    delayedHide(AUTO_HIDE_DELAY_MILLIS);
    }
    return false;
    }
    }; Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
    mSystemUiHider.hide();
    }
    }; /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
    //以下是测试网络的
    public boolean isNetworkConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
    if (mNetworkInfo != null) { 
    return mNetworkInfo.isAvailable(); 


    return false; 

    }
    public boolean isWifiConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mWiFiNetworkInfo = mConnectivityManager 
    .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    if (mWiFiNetworkInfo != null) { 
    return mWiFiNetworkInfo.isAvailable(); 


    return false; 
    }
    public boolean isMobileConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mMobileNetworkInfo = mConnectivityManager 
    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
    if (mMobileNetworkInfo != null) { 
    return mMobileNetworkInfo.isAvailable(); 


    return false; 
    }
    //测试网络结束
    }
      

  3.   

    if (isNetworkConnected(this)) {
    try {
    Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("test");
    alert.setMessage("test");
    alert.setIcon(android.R.drawable.ic_menu_info_details);
    alert.setPositiveButton("OK",
    new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
     
    MainActivity.this.finish();  

    }
    });
    alert.setNegativeButton("cancel",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { dialog.dismiss();
    }
    }); alert.create().show();
    } catch (Exception e) { } catch (Throwable e) { }
    }
      

  4.   

    谢谢您的回复,请问这段代码加在哪呢,我加在175行以后,很多错误,根据eclipse 的种种提示修改以后还是有错,请指教,谢谢!
      

  5.   


    我修改后的代码如下,不出错了,但是关闭所有网络之后测试时,也没有提示任何错误,请指教,谢谢!package comhy.example.hysjmall;import comhy.example.hysjmall.util.SystemUiHider;
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.MotionEvent;
    import android.view.View;/**
     * An example full-screen activity that shows and hides the system UI (i.e.
     * status bar and navigation/system bar) with user interaction.
     * 
     * @see SystemUiHider
     */public class FullscreenActivity extends Activity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true; /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000; /**
     * If set, will toggle the system UI visibility upon interaction. Otherwise,
     * will show the system UI visibility upon interaction.
     */
    private static final boolean TOGGLE_ON_CLICK = true; /**
     * The flags to pass to {@link SystemUiHider#getInstance}.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; /**
     * The instance of the {@link SystemUiHider} for this activity.
     */
    private SystemUiHider mSystemUiHider; @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen); final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content); // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView,
    HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
    .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
    // Cached values.
    int mControlsHeight;
    int mShortAnimTime; @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public void onVisibilityChange(boolean visible) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    // If the ViewPropertyAnimator API is available
    // (Honeycomb MR2 and later), use it to animate the
    // in-layout UI controls at the bottom of the
    // screen.
    if (mControlsHeight == 0) {
    mControlsHeight = controlsView.getHeight();
    }
    if (mShortAnimTime == 0) {
    mShortAnimTime = getResources().getInteger(
    android.R.integer.config_shortAnimTime);
    }
    controlsView
    .animate()
    .translationY(visible ? 0 : mControlsHeight)
    .setDuration(mShortAnimTime);
    } else {
    // If the ViewPropertyAnimator APIs aren't
    // available, simply show or hide the in-layout UI
    // controls.
    controlsView.setVisibility(visible ? View.VISIBLE
    : View.GONE);
    } if (visible && AUTO_HIDE) {
    // Schedule a hide().
    delayedHide(AUTO_HIDE_DELAY_MILLIS);
    }
    }
    }); // Set up the user interaction to manually show or hide the system UI.
    contentView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    if (TOGGLE_ON_CLICK) {
    mSystemUiHider.toggle();
    } else {
    mSystemUiHider.show();
    }
    }
    }); // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    findViewById(R.id.dummy_button).setOnTouchListener(
    mDelayHideTouchListener);
    } @Override
    protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState); // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
    } /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
    if (AUTO_HIDE) {
    delayedHide(AUTO_HIDE_DELAY_MILLIS);
    }

    return false;
    }
    }; Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
    mSystemUiHider.hide();
    }
    }; /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }

    //以下是测试网络的
    public boolean isNetworkConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
    if (mNetworkInfo != null) { 
    return mNetworkInfo.isAvailable(); 


    if (isNetworkConnected(this)) {
    try {
    android.app.AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("test");
    alert.setMessage("test");
    alert.setIcon(android.R.drawable.ic_menu_info_details);
    alert.setNegativeButton("OK",
    new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
     
    FullscreenActivity.this.finish();  

    }
    });
    alert.setNegativeButton("cancel",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { dialog.dismiss();
    }
    }); alert.create().show();
    } catch (Exception e) { } catch (Throwable e) { }
    }
    return false; 

    }

    public boolean isWifiConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mWiFiNetworkInfo = mConnectivityManager 
    .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    if (mWiFiNetworkInfo != null) { 
    return mWiFiNetworkInfo.isAvailable(); 


    return false; 
    }
    public boolean isMobileConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mMobileNetworkInfo = mConnectivityManager 
    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
    if (mMobileNetworkInfo != null) { 
    return mMobileNetworkInfo.isAvailable(); 


    return false; 
    }

    //测试网络结束

    }
      

  6.   

    关掉网络之后,这样也不行,
    package comhy.example.hysjmall;import comhy.example.hysjmall.util.SystemUiHider;
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.MotionEvent;
    import android.view.View;/**
     * An example full-screen activity that shows and hides the system UI (i.e.
     * status bar and navigation/system bar) with user interaction.
     * 
     * @see SystemUiHider
     */public class FullscreenActivity extends Activity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true; /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000; /**
     * If set, will toggle the system UI visibility upon interaction. Otherwise,
     * will show the system UI visibility upon interaction.
     */
    private static final boolean TOGGLE_ON_CLICK = true; /**
     * The flags to pass to {@link SystemUiHider#getInstance}.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; /**
     * The instance of the {@link SystemUiHider} for this activity.
     */
    private SystemUiHider mSystemUiHider; @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen); final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content); // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView,
    HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
    .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
    // Cached values.
    int mControlsHeight;
    int mShortAnimTime; @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public void onVisibilityChange(boolean visible) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    // If the ViewPropertyAnimator API is available
    // (Honeycomb MR2 and later), use it to animate the
    // in-layout UI controls at the bottom of the
    // screen.
    if (mControlsHeight == 0) {
    mControlsHeight = controlsView.getHeight();
    }
    if (mShortAnimTime == 0) {
    mShortAnimTime = getResources().getInteger(
    android.R.integer.config_shortAnimTime);
    }
    controlsView
    .animate()
    .translationY(visible ? 0 : mControlsHeight)
    .setDuration(mShortAnimTime);
    } else {
    // If the ViewPropertyAnimator APIs aren't
    // available, simply show or hide the in-layout UI
    // controls.
    controlsView.setVisibility(visible ? View.VISIBLE
    : View.GONE);
    } if (visible && AUTO_HIDE) {
    // Schedule a hide().
    delayedHide(AUTO_HIDE_DELAY_MILLIS);
    }
    }
    }); // Set up the user interaction to manually show or hide the system UI.
    contentView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    if (TOGGLE_ON_CLICK) {
    mSystemUiHider.toggle();
    } else {
    mSystemUiHider.show();
    }
    }
    }); // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    findViewById(R.id.dummy_button).setOnTouchListener(
    mDelayHideTouchListener);
    } @Override
    protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState); // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
    } /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
    if (AUTO_HIDE) {
    delayedHide(AUTO_HIDE_DELAY_MILLIS);
    }

    return false;
    }
    }; Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
    mSystemUiHider.hide();
    }
    }; /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }

    //以下是测试网络的
    public boolean isNetworkConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
    if (mNetworkInfo != null) { 
    return mNetworkInfo.isAvailable(); 



    //提示
    try {
    android.app.AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("test");
    alert.setMessage("test");
    alert.setIcon(android.R.drawable.ic_menu_info_details);
    alert.setNegativeButton("OK",
    new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
     
    FullscreenActivity.this.finish();  

    }
    });
    alert.setNegativeButton("cancel",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { dialog.dismiss();
    }
    }); alert.create().show();
    } catch (Exception e) { } catch (Throwable e) { }
    //提示结束
    return false; 

    }

    public boolean isWifiConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mWiFiNetworkInfo = mConnectivityManager 
    .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    if (mWiFiNetworkInfo != null) { 
    return mWiFiNetworkInfo.isAvailable(); 


    //提示

    android.app.AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("test");
    alert.setMessage("test");
    alert.setIcon(android.R.drawable.ic_menu_info_details);
    alert.setNegativeButton("OK",
    new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
     
    FullscreenActivity.this.finish();  

    }
    });
    alert.setNegativeButton("cancel",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { dialog.dismiss();
    }
    }); alert.create().show();


    //提示结束
    return false; 
    }
    public boolean isMobileConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mMobileNetworkInfo = mConnectivityManager 
    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
    if (mMobileNetworkInfo != null) { 
    return mMobileNetworkInfo.isAvailable(); 


    return false; 
    }

    //测试网络结束

    }
      

  7.   

    看来你还真是个新手,怎么能把判断放在方法里面呢。这段直接加到oncreat里面就行
    if (isNetworkConnected(this)) {
                try {
                    android.app.AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    alert.setTitle("test");
                    alert.setMessage("test");
                    alert.setIcon(android.R.drawable.ic_menu_info_details);
                    alert.setNegativeButton("OK",
                            new DialogInterface.OnClickListener() {
     
                                public void onClick(DialogInterface dialog, int which) {
                                  
                                        FullscreenActivity.this.finish();                                         
                                     
                                }
                            });
                    alert.setNegativeButton("cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
     
                                    dialog.dismiss();
                                }
                            });
     
                    alert.create().show();
                } catch (Exception e) {
     
                } catch (Throwable e) {
     
                }
      

  8.   


    必须再次感谢您的回贴!能否根据我4楼的代码,大概指教我一下具体放在哪,我放在 fullscreenactivity.java中的
    protected void onCreate(Bundle savedInstanceState) {
    的开头,结尾,和中间,都提示错误
      

  9.   

    setContentView(R.layout.activity_fullscreen);
    放这句后面会报错嘛
      

  10.   

    会的,mSystemUiHider
    .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() { 这句以下开始报错
    如下图,
      

  11.   

    谢谢你不倦的指导,FullscreenActivity.java源码如下:
    package comhy.example.hysjmall;import comhy.example.hysjmall.util.SystemUiHider;
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.MotionEvent;
    import android.view.View;/**
     * An example full-screen activity that shows and hides the system UI (i.e.
     * status bar and navigation/system bar) with user interaction.
     * 
     * @see SystemUiHider
     */public class FullscreenActivity extends Activity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true; /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000; /**
     * If set, will toggle the system UI visibility upon interaction. Otherwise,
     * will show the system UI visibility upon interaction.
     */
    private static final boolean TOGGLE_ON_CLICK = true; /**
     * The flags to pass to {@link SystemUiHider#getInstance}.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; /**
     * The instance of the {@link SystemUiHider} for this activity.
     */
    private SystemUiHider mSystemUiHider; @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen);
    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content); // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView,
    HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
    .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
    // Cached values.
    int mControlsHeight;
    int mShortAnimTime; @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    public void onVisibilityChange(boolean visible) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    // If the ViewPropertyAnimator API is available
    // (Honeycomb MR2 and later), use it to animate the
    // in-layout UI controls at the bottom of the
    // screen.
    if (mControlsHeight == 0) {
    mControlsHeight = controlsView.getHeight();
    }
    if (mShortAnimTime == 0) {
    mShortAnimTime = getResources().getInteger(
    android.R.integer.config_shortAnimTime);
    }
    controlsView
    .animate()
    .translationY(visible ? 0 : mControlsHeight)
    .setDuration(mShortAnimTime);
    } else {
    // If the ViewPropertyAnimator APIs aren't
    // available, simply show or hide the in-layout UI
    // controls.
    controlsView.setVisibility(visible ? View.VISIBLE
    : View.GONE);
    } if (visible && AUTO_HIDE) {
    // Schedule a hide().
    delayedHide(AUTO_HIDE_DELAY_MILLIS);
    }
    }

    }
    );
    // Set up the user interaction to manually show or hide the system UI.
    contentView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    if (TOGGLE_ON_CLICK) {
    mSystemUiHider.toggle();
    } else {
    mSystemUiHider.show();
    }
    }
    }); // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    findViewById(R.id.dummy_button).setOnTouchListener(
    mDelayHideTouchListener);

    } @Override
    protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState); // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
    } /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
    if (AUTO_HIDE) {
    delayedHide(AUTO_HIDE_DELAY_MILLIS);
    }

    return false;
    }
    }; Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
    mSystemUiHider.hide();
    }
    }; /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }

    //以下是测试网络的
    public boolean isNetworkConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
    if (mNetworkInfo != null) { 
    return mNetworkInfo.isAvailable(); 



    //提示
    try {
    android.app.AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("test");
    alert.setMessage("test");
    alert.setIcon(android.R.drawable.ic_menu_info_details);
    alert.setNegativeButton("OK",
    new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
     
    FullscreenActivity.this.finish();  

    }
    });
    alert.setNegativeButton("cancel",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { dialog.dismiss();
    }
    }); alert.create().show();
    } catch (Exception e) { } catch (Throwable e) { }
    //提示结束
    return false; 

    }

    public boolean isWifiConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mWiFiNetworkInfo = mConnectivityManager 
    .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    if (mWiFiNetworkInfo != null) { 
    return mWiFiNetworkInfo.isAvailable(); 


    //提示

    android.app.AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("test");
    alert.setMessage("test");
    alert.setIcon(android.R.drawable.ic_menu_info_details);
    alert.setNegativeButton("OK",
    new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
     
    FullscreenActivity.this.finish();  

    }
    });
    alert.setNegativeButton("cancel",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { dialog.dismiss();
    }
    }); alert.create().show();


    //提示结束
    return false; 
    }
    public boolean isMobileConnected(Context context) { 
    if (context != null) { 
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
    .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mMobileNetworkInfo = mConnectivityManager 
    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
    if (mMobileNetworkInfo != null) { 
    return mMobileNetworkInfo.isAvailable(); 


    return false; 
    }

    //测试网络结束

    }这个项目其实我只进行到新建了一个全屏项目,在activity_fullscreen.xml中拖拽了一个WebView的控件,只进行到这里了。我还没摸索到怎么设置WebView显示的网址,我想运行这个 app时,先检查网络连接,如果没有网络,就给一个对话框,然后退出应用,不显示给用户  找不到服务器 那种网页错误提示。现在第一步,检测网络,给出对话框 就卡住了。。真的是太新手了,也没有时间来不及去从java再学起的,