2

我想检查网络状态,如果网络未连接意味着我将在我的页面中显示警报对话框。

这是我的代码

@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);<br>
    setContentView(R.layout.activity_main);  

    // make the activity visible 
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {              
             ServicesLayer objService = new ServicesLayer();
             boolean status = objService.connectedToNetwork(this);
             System.out.println("status: " + status);
             if(objService.connectedToNetwork(this)){

                callService();
             }else{

                System.out.println("Inside Network Else");

                // Display message and set flag not connected
                showAlertDialog(this, "Network Connection", "Connection to network is required for proper use of the iHelpPlus. Would you like to enable it now?" );
             }
        }
    }, 1000);    
}   

@SuppressWarnings("deprecation")
public void showAlertDialog(Context context, String title, String message) {<br/>
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();<br/>

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);


    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

如果网络已连接意味着它工作正常......否则应用程序将关闭并出现强制关闭错误。

如何在我的应用程序中解决上述问题?

请任何人帮助我

4

0 回答 0