我目前正在做一个基于网络连接的项目。我正在开发一个定期检查网络连接的应用程序。如果没有连接,进度对话框应旋转显示“无网络连接”,直到用户自己转动打开wifi或任何其他类型的互联网连接。打开wifi后,如果应用程序与wifi连接,则进度对话框应关闭,控件应传递给另一个活动。我在谷歌搜索了很多次,但没有得到满意的答案。以下是我的代码:
public class Alert extends Activity implements Runnable{
ProgressDialog pd;
WifiManager wm,wifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert);
wm = (WifiManager) getSystemService(WIFI_SERVICE);
if(!wm.isWifiEnabled()) {
pd = ProgressDialog.show(this, "Network Error!!", "Network not found.Please make sure there is presence of any kind of network connection!!");
Thread t = new Thread(this);
t.start();
}
}
@Override
protected void onResume() {
super.onResume();
if(wm.isWifiEnabled()) {
pd.dismiss();
Intent in=new Intent(Alert.this,WebPageView.class);
startActivity(in);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(wm.getWifiState() != 3) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
答案 0 :(得分:1)
To check network connection, use ConnectivityManager class.
Add below method to your activity and call it to check network connection, if it returns true then it means network is available otherwise not.
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
Now do it in your onCreate(...) method
if(!isNetworkAvailable(Alert.this)) {
pd = ProgressDialog.show(this, "Network Error!!", "Network not found.Please make sure there is presence of any kind of network connection!!");
Thread t = new Thread(this);
t.start();
}else{
if(pd != null && pd.isShowing()){
pd.dismiss();
}
Intent in=new Intent(Alert.this,WebPageView.class);
startActivity(in);
}
Hope this will help you.
答案 1 :(得分:0)
答案 2 :(得分:0)
使用Handler尝试这样做,它可能会有效。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert);
wm = (WifiManager) getSystemService(WIFI_SERVICE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if(!wm.isWifiEnabled()) {
runOnUiThread(new Runnable() {
public void run() {
if(pd == null)
pd = ProgressDialog.show(this, "Network Error!!", "Network not found.Please make sure there is presence of any kind of network connection!!");
}
});
}else{
runOnUiThread(new Runnable() {
public void run() {
if(pd != null)
pd.dismiss();
Intent in=new Intent(Alert.this,WebPageView.class);
startActivity(in);
}
});
}
handler.postDelayed(this, 5000); //now is every 2 minutes
}
}, 5000);
}
答案 3 :(得分:0)
@Override
public void run() {
// TODO Auto-generated method stub
while(running) {
try {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (info.isConnected()) {
// you got a connection! tell your user!
Log.i("Post", "Connected");
running=false;
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
pd.dismiss();
}
});
Intent intent_service=new Intent(this, anotherActivity.class);
context.startService(intent_service);
}
}
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 4 :(得分:0)
将此课程放在您的项目中:
public class Utility {
public static boolean isNetworkConnected(Context context){
boolean connected = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
connected = (cm.getActiveNetworkInfo() != null&&cm.getActiveNetworkInfo().isAvailable() && cm
.getActiveNetworkInfo().isConnected());
return connected;
}
public static void showAlert(final Activity activity, final String message,final String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message).setTitle(title).setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
activity.finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public static void showAlertValidation(final Activity activity,final String message,final String title){
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message).setTitle(title).setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
此课程会检测您的互联网连接是否已开启。在您的活动之后,输入以下代码:
if(Utility.isNetworkConnected(Alert.this))
{
Intent in=new Intent(Alert.this,WebPageView.class);
startActivity(in);
}
else if(!Utility.isNetworkConnected(Alert.this))
Utility.showAlert(Alert.this,"Internet Connection Not Present.","Network Error!");
检查并确定问题是否已解决。