你能帮我解决这个问题吗?
oncreate
如果 Internet 连接可用,如何创建以方法运行 url 的活动?
我想每 5 秒检查一次 Internet 连接是否可用,如果 Internet 连接消失,它只会提示一次对话框。
如果 Internet 连接到来,那么活动将在会话关闭的地方恢复
下面我的代码是:
public class MainActivity extends Activity {
private String URL="http://www.moneycontrol.com/";
private Handler mHandler = new Handler();
private boolean isRunning = true;
private TextView textlabel=null;
public WebView webview=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textlabel=(TextView)findViewById(R.id.textlabel);
displayData();
}
private void Chekstate() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
try {
Thread.sleep(1000);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
displayData();
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
}
@SuppressWarnings("deprecation")
private void displayData() {
ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf=cn.getActiveNetworkInfo();
if(nf != null && nf.isConnected()==true )
{
textlabel.setText("Network Available");
Webview();
}
else
{
textlabel.setText("Network Not Available");
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Intenet Connecction");
alertDialog.setMessage("Internet Connetion is not available now.");
alertDialog.setButton(" OK ", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
alertDialog.show();
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@SuppressLint("SetJavaScriptEnabled")
private void Webview() {
webview= (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(URL);
webview.setInitialScale(1);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setUseWideViewPort(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}