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