我收到以下错误:
ACTION_WIRELESS_SETTINGS
无法解析或不属于字段(build = new Builder(Context); )
无法将上下文解析为变量(build = new Builder(Context); )
类型不匹配:cannot convert from AnimatorSet.Builder to AlertDialog.Builder( startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));)
SplashScreen.java
public class SplashScreen extends Activity {
static ConnectivityManager cm;
AlertDialog dailog;
AlertDialog.Builder build;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
build = new Builder(Context);
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting()
|| cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting()// if connection is
// there screen goes
// to next screen
// else shows
// message toast
) {
Log.e("cm value", "" + cm.getAllNetworkInfo().toString());
Toast.makeText(SplashScreen.this, "Internet is active", 2000)
.show();
Thread mythread = new Thread() {
public void run() {
try {
sleep(5000);
} catch (Exception e) {
} finally {
Intent intent = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
};
mythread.start();
} else {
build.setMessage("This application requires Internet connection.Would you connect to internet ?");
build.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
build.setNegativeButton("No", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
build.setMessage("Are sure you want to exit?");
build.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
build.setNegativeButton("NO", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
Intent intent = new Intent(SplashScreen.this,
SplashScreen.class);
startActivity(intent);
dialog.dismiss();
}
});
dailog = build.create();
dailog.show();
}
});
dailog = build.create();
dailog.show();
}
}
@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 :(得分:0)
这是一个帮助类:
public class NetworkManager extends BroadcastReceiver {
// LOG
protected static final String TAG = NetworkManager.class.getSimpleName();
// DATA
protected Context mContext;
protected boolean mNoConnectivity;
protected String mReason;
protected boolean mIsFailover;
protected static boolean mIsConnected = false;
protected static boolean mIsConnectivityGood = true;
public NetworkManager(Context context) {
this.mContext = context;
}
public void registerReceivers() {
mContext.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
public static NetworkInfo getNetworkInfo(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
}
public static boolean isConnected(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected());
}
public static boolean isConnectedWifi(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
public static boolean isConnectedMobile(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
public static boolean isConnectedFast(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected() && isConnectionFast(info.getType(), info.getSubtype()));
}
protected static boolean isConnectionFast(int type, int subType) {
if (type == ConnectivityManager.TYPE_WIFI) {
return true;
}
else if (type == ConnectivityManager.TYPE_MOBILE) {
switch (subType) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return false; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return true; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return true; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return false; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return true; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return true; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return true; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return true; // ~ 400-7000 kbps
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return false;
}
}
else {
return false;
}
}
@Override
public void onReceive(Context context, Intent intent) {
mNoConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
mReason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
mIsFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
//
if (mNoConnectivity) {
mIsConnected = false;
}
else {
if (isConnectedFast(mContext)) {
mIsConnectivityGood = true;
}
else {
mIsConnectivityGood = false;
}
mIsConnected = true;
}
}
}
你可以使用广播接收器来获取活动中的事件,或者使用静态方法来检查连接性。
答案 1 :(得分:0)
使用此功能检查设备是否具有互联网..
public static boolean hasInternet(Activity a) {
boolean hasConnectedWifi = false;
boolean hasConnectedMobile = false;
try {
ConnectivityManager cm = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("wifi"))
if (ni.isConnected())
hasConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("mobile"))
if (ni.isConnected())
hasConnectedMobile = true;
}
return hasConnectedWifi || hasConnectedMobile;
}
catch (Exception ex) {
CommonUtils.postException(ex, ex.getMessage());
}
return false;
}