Java 检测到网络连接时启动另一个活动

Java 检测到网络连接时启动另一个活动,java,android,eclipse,networking,android-studio,Java,Android,Eclipse,Networking,Android Studio,我目前正在做一个基于网络连接的项目。我正在开发一个定期检查网络连接的应用程序。如果没有连接,进度对话框应该旋转显示“没有网络连接”直到用户自己打开wifi或任何其他类型的互联网连接。打开wifi后,如果应用程序与wifi连接,则进度对话框应关闭,控件应传递给其他活动。我在谷歌上搜索了很多次,但都没有得到满意的答案。以下是我的代码: public class Alert extends Activity implements Runnable{ ProgressDia

我目前正在做一个基于网络连接的项目。我正在开发一个定期检查网络连接的应用程序。如果没有连接,进度对话框应该旋转显示“没有网络连接”直到用户自己打开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();
                }

            }

            }
    }
 }
看看这个

根据internet状态的变化,您可以调用您的活动


希望这将解决您的问题

使用处理程序尝试这样做,可能会奏效

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

    }

将该类放入项目中:

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();
    }
}
此类检测您的internet连接是否打开。之后,在活动中输入以下代码:

    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!");

查看您的问题是否得到解决。

看起来更像是一个评论,而不是回答。他正在定期检查互联网、wifi或其他网络。这一刻,他可以检查互联网状态。这是检查internet状态的正确方法。然后,他可以调用特定的活动。在5秒钟的间隔内,它会连续崩溃,并自动打开,然后再次崩溃,就像在循环中一样。我编辑了我的答案。请检查并重播我的答案。
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.
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.