android

时间:2016-08-09 07:50:00

标签: android

SplashScreenActivity.java

package com.example.wastewarriorsnew;


public class SplashScreenActivity extends Activity {

 ConnectivityManager connectivityManager;
    NetworkInfo info;

 // Splash screen timer
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_splash_screen);


   Timer t = new Timer();
   boolean checkConnection=new SplashScreenActivity().checkConnection(this);
   if (checkConnection) {
       t.schedule(new splash(), 1000);
   } else {
       Toast.makeText(SplashScreenActivity.this,
               "connection not found...plz check connection", 1000).show();
   }
  }

 class splash extends TimerTask {

   @Override
   public void run() {
       Intent i = new Intent(SplashScreenActivity.this,MainActivity.class);
       finish();
       startActivity(i);
   }
  }

public boolean checkConnection(Context context) {
    // TODO Auto-generated method stub
     boolean flag = false;
        try {
            connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            info = connectivityManager.getActiveNetworkInfo();

            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                System.out.println(info.getTypeName());
                flag = true;
            }
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                System.out.println(info.getTypeName());
                flag = true;
            }
        } catch (Exception exception) {
            System.out.println("Exception at network connection....."
                    + exception);
        }
        return flag;

    //return false;
 }
 }

即使在此之后,我也没有收到Toast消息,即互联网连接已关闭。 我不明白什么是错的。 请帮我解决一下。 我没有收到任何错误,但没有互联网连接,应用程序正在运行并显示主要活动。 当我点击“保存”按钮时,很遗憾地停止了。

3 个答案:

答案 0 :(得分:0)

您可以使用@Thommy的互联网连接响应。别忘了在清单中添加此权限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

然后检查如下:

if(hasActiveInternetConnection(context)){
//do anything
}else{
Toast.makeText(SplashScreenActivity.this,
               "connection not found...plz check connection", Toast.LENGTH_LONG).show();
}

答案 1 :(得分:0)

使用此代码了解设备是否已连接到互联网! 如果没有连接,则显示一个警告对话框,导航到设置以启用INTERNET。

android.net.ConnectivityManager cm = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);

   android.net.NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
    if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
        Toast.makeText(MainActivity.this, "INTERNET is Available", Toast.LENGTH_SHORT).show();
    } else {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.internet_logo);
        builder.setTitle("NO INTERNET");
        builder.setMessage("Please Check Your Internet Connection.");
        builder.setPositiveButton("Enable INTERNET", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                startActivity(intent);

            }
        });
        builder.show();

    }

答案 2 :(得分:0)

您可以像这样检查有效的Internet连接:

    public boolean hasActiveInternetConnection(Context context) {
    if(this.isAnyNetworkConnected(context)) {
        try {
            HttpURLConnection e = (HttpURLConnection)((HttpURLConnection)(new URL("http://www.google.com")).openConnection());
            e.setRequestProperty("User-Agent", "Test");
            e.setRequestProperty("Connection", "close");
            e.setConnectTimeout(1500);
            e.connect();
            return e.getResponseCode() == 200;
        } catch (IOException var3) {
            ;
        }
    }

    return false;
}

也不要为此使用Time,也不要自己实例化Activity类(如new SplashScreenActivity()