我在stackoverflow上找到了这段代码..
<gap:config-file platform="android" parent="/manifest">
<application android:debuggable="true" />
</gap:config-file>
我只是想知道如何将此代码实现到我的index_tivity的代码中。
这是我的package com.emil.android.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
* Check device's network connectivity and speed
* @author emil http://stackoverflow.com/users/220710/emil
*
*/
public class Connectivity {
/**
* Get the network info
* @param context
* @return
*/
public static NetworkInfo getNetworkInfo(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
}
/**
* Check if there is any connectivity
* @param context
* @return
*/
public static boolean isConnected(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected());
}
/**
* Check if there is any connectivity to a Wifi network
* @param context
* @param type
* @return
*/
public static boolean isConnectedWifi(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
/**
* Check if there is any connectivity to a mobile network
* @param context
* @param type
* @return
*/
public static boolean isConnectedMobile(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
/**
* Check if there is fast connectivity
* @param context
* @return
*/
public static boolean isConnectedFast(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
}
/**
* Check if the connection is fast
* @param type
* @param subType
* @return
*/
public 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
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
return true; // ~ 1-2 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
return true; // ~ 5 Mbps
case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
return true; // ~ 10-20 Mbps
case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
return false; // ~25 kbps
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return true; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return false;
}
}else{
return false;
}
}
}
indexactivity
在我的indexactivity中,我使用此代码检查连接..
package com.example.administrator.mosbeau;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
@SuppressWarnings("deprecation")
public class IndexActivity extends Activity {
Button joinbutton, signbutton;
UserLocalStore userLocalStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
//boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
if(isConnected){
}else{
nointernet();
}
userLocalStore = new UserLocalStore(this);
// Locate the button in activity_main.xml
joinbutton = (Button) findViewById(R.id.joinbutton);
signbutton = (Button) findViewById(R.id.signbutton);
// Capture button clicks
joinbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(IndexActivity.this, RegisterActivity.class);
startActivity(myIntent);
}
});
signbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(IndexActivity.this, LoginActivity.class);
startActivity(myIntent);
}
});
}
public void onStart() {
super.onStart();
if(authenticate() == true){
/*Intent myIntent = new Intent(IndexActivity.this, MainActivity.class);
startActivity(myIntent);*/
displayUserDetails();
}
}
private boolean authenticate() {
if (userLocalStore.getLoggedInUser() == null) {
return false;
}
return true;
}
private void displayUserDetails(){
User user = userLocalStore.getLoggedInUser();
if(user.customers_id==""){
}else{
Intent myIntent = new Intent(IndexActivity.this, MainActivity.class);
startActivity(myIntent);
}
}
public void nointernet(){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setMessage("There seems to be a problem with your connection.");
dialogBuilder.setNegativeButton("Edit Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
dialogBuilder.setPositiveButton("Reload", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
AlertDialog dialog = dialogBuilder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
}
}
如何使用上面的代码并在每次返回false时显示消息?
答案 0 :(得分:2)
您可以在onCreate中替换这些行
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
//boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
if(isConnected){
}else{
nointernet();
}
到
Connectivity connectivity=new Connectivity();
if(connectivity.isConnected(IndexActivity.this))
{
}else{
nointernet();
}