如何解决android中的致命异常Timer-0?

时间:2012-12-21 11:15:12

标签: android android-networking

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

这是我的代码

@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();
}

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

如何在我的申请中解决上述问题?

请任何人帮助我

0 个答案:

没有答案