你可以帮我解决这个问题吗?
如果互联网连接可用,如何创建以oncreate
方法运行网址的活动?
我想检查互联网连接是否每5秒钟一次,如果互联网连接失效,它只会在稀释箱中提醒一次。
如果互联网连接出现,那么会话将在会话结束时恢复
下面我的代码是:
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;
}
}
答案 0 :(得分:1)
要检查互联网连接,您可以使用以下内容。
在清单
中添加以下权限 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
使用处理程序
Handler mHandler = new Handler();
boolean isRunning = true;
然后,使用onCreate()方法中的这个线程:
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
try {
Thread.sleep(5000);
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();
然后,声明这个方法,由你的处理程序每隔5秒调用一次:
private void displayData() {
ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf=cn.getActiveNetworkInfo();
if(nf != null && nf.isConnected()==true )
{
Toast.makeText(this, "Network Available", Toast.LENGTH_SHORT).show();
myTextView.setText("Network Available");
}
else
{
Toast.makeText(this, "Network Not Available", Toast.LENGTH_SHORT).show();
myTextView.setText("Network Not Available");
}
}
要停止线程调用:
isRunning = false;
另外你可以使用下面的内容。但它不会检查每5个连接的连接性
public class CheckNetwork {
private static final String TAG = CheckNetwork.class.getSimpleName();
public static boolean isInternetAvailable(Context context)
{
NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null)
{
Log.d(TAG,"no internet connection");
return false;
}
else
{
if(info.isConnected())
{
Log.d(TAG," internet connection available...");
return true;
}
else
{
Log.d(TAG," internet connection");
return true;
}
}
}
}
在onCreate()
中的活动中if(CheckNetwork.isInternetAvailable(MainActivtiy.this)) //if connection available
{
}
http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
您可以使用BroadCast Reciever。当连接丢失时,系统广播。但我建议不要使用广播接收器。