Example usage for android.net NetworkInfo isConnected

List of usage examples for android.net NetworkInfo isConnected

Introduction

In this page you can find the example usage for android.net NetworkInfo isConnected.

Prototype

@Deprecated
public boolean isConnected() 

Source Link

Document

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Usage

From source file:org.protocoderrunner.apprunner.api.PNetwork.java

@ProtocoderScript
@APIMethod(description = "Check if internet connection is available", example = "")
@APIParam(params = { "" })
public boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) a.get()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:com.phonegap.DroidGap.java

private boolean HInternet() {
    boolean result = false;

    ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = connManager.getActiveNetworkInfo();

    if (info == null || !info.isConnected()) {
        result = false;//from w w  w .  j a va  2s  .c o m
    } else {
        if (!info.isAvailable()) {
            result = false;
        } else {
            result = true;
        }
    }

    return result;
}

From source file:com.kncwallet.wallet.ui.WalletActivity.java

private boolean isNetworkAvailable() {
    android.net.ConnectivityManager connectivityManager = (android.net.ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:com.xmobileapp.rockplayer.LastFmAlbumArtImporter.java

/*********************************
 * /*  ww w  .j a v  a  2 s.com*/
 * Constructor
 * @param context
 * 
 *********************************/
public LastFmAlbumArtImporter(Context context) {
    this.context = context;
    try {
        /*
         * Check for Internet Connection (Through whichever interface)
         */
        ConnectivityManager connManager = (ConnectivityManager) ((RockPlayer) context)
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connManager.getActiveNetworkInfo();
        /******* EMULATOR HACK - false condition needs to be removed *****/
        //if (false && (netInfo == null || !netInfo.isConnected())){
        if ((netInfo == null || netInfo.isConnected() == false)) {
            Bundle data = new Bundle();
            data.putString("info", "No Internet Connection");
            Message msg = new Message();
            msg.setData(data);
            ((RockPlayer) context).getAlbumArtHandler.sendMessage(msg);
            return;
        }

        /*
         * Import Album Art from Last FM
         */
        //getAlbumArt();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.edible.ocr.CaptureActivity.java

public boolean isConnected() {
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected())
        return true;
    else//from   ww w  .j a v a2 s  . c  om
        return false;
}

From source file:com.southernstorm.tvguide.TvChannelCache.java

/**
 * Determine if networking is available at the present time.
 * /*w  w w .ja va 2 s  .  c  o m*/
 * @return true if networking is available, false if not (e.g. airplane mode).
 */
public boolean isNetworkingAvailable() {
    Context context = getContext();
    if (context == null)
        return false;
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager == null)
        return false;
    NetworkInfo info = manager.getActiveNetworkInfo();
    if (info == null)
        return false;
    return info.isAvailable() && info.isConnected();
}

From source file:org.nla.tarotdroid.lib.ui.GameSetHistoryActivity.java

/**
 * Manage click on GameSet.//from www  . j av  a 2  s .  com
 * 
 * @param pos
 */
private void onListItemClick(final int pos) {
    final GameSet gameSet = (GameSet) getListAdapter().getItem(pos);

    final Item[] items = AppContext.getApplication().isAppLimited() ? limitedItems : allItems;

    ListAdapter adapter = new ArrayAdapter<Item>(this, android.R.layout.select_dialog_item, android.R.id.text1,
            items) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // User super class to create the View
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView) v.findViewById(android.R.id.text1);

            // Put the image on the TextView
            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);

            // Add margin between image and text (support various screen
            // densities)
            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            tv.setCompoundDrawablePadding(dp5);

            return v;
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(String.format(this.getString(R.string.lblGameSetHistoryActivityMenuTitle),
            new SimpleDateFormat("dd/MM/yy").format(gameSet.getCreationTs())));

    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int itemIndex) {

            Item item = items[itemIndex];

            if (item.itemType == Item.ItemTypes.publishOnFacebook) {

                // check for active internet connexion first
                // see post
                // http://stackoverflow.com/questions/2789612/how-can-i-check-whether-an-android-device-is-connected-to-the-web
                ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
                        Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

                if (networkInfo != null && networkInfo.isConnected()) {

                    if (gameSet.getGameCount() == 0) {
                        Toast.makeText(GameSetHistoryActivity.this,
                                R.string.lblFacebookImpossibleToPublishGamesetWithNoGame, Toast.LENGTH_SHORT)
                                .show();
                    }

                    else if (!AppContext.getApplication().getNotificationIds().isEmpty()) {
                        Toast.makeText(GameSetHistoryActivity.this, R.string.lblFacebookGamesetBeingPublished,
                                Toast.LENGTH_SHORT).show();
                    }

                    else {
                        tempGameSet = gameSet;

                        // // TODO Improve in later version
                        // ShortenUrlTask shortenUrlTask = new
                        // ShortenUrlTask(FacebookHelper.buildGameSetUrl(tempGameSet));
                        // shortenUrlTask.setCallback(urlShortenedCallback);
                        // shortenUrlTask.execute();

                        startPostProcess();
                    }
                } else {
                    Toast.makeText(GameSetHistoryActivity.this,
                            getString(R.string.titleInternetConnexionNecessary), Toast.LENGTH_LONG).show();
                }
            } else if (item.itemType == Item.ItemTypes.publishOnTwitter) {
                Toast.makeText(GameSetHistoryActivity.this, "TODO: Publish on twitter", Toast.LENGTH_LONG)
                        .show();

                Intent intent = new Intent(GameSetHistoryActivity.this, TwitterConnectActivity.class);
                startActivity(intent);
            } else if (item.itemType == Item.ItemTypes.remove) {
                RemoveGameSetDialogClickListener removeGameSetDialogClickListener = new RemoveGameSetDialogClickListener(
                        gameSet);
                AlertDialog.Builder builder = new AlertDialog.Builder(GameSetHistoryActivity.this);
                builder.setTitle(GameSetHistoryActivity.this.getString(R.string.titleRemoveGameSetYesNo));
                builder.setMessage(Html.fromHtml(
                        GameSetHistoryActivity.this.getText(R.string.msgRemoveGameSetYesNo).toString()));
                builder.setPositiveButton(GameSetHistoryActivity.this.getString(R.string.btnOk),
                        removeGameSetDialogClickListener);
                builder.setNegativeButton(GameSetHistoryActivity.this.getString(R.string.btnCancel),
                        removeGameSetDialogClickListener).show();
                builder.setIcon(android.R.drawable.ic_dialog_alert);
            } else if (item.itemType == Item.ItemTypes.transferOverBluetooth) {
                if (!GameSetHistoryActivity.this.bluetoothHelper.isBluetoothEnabled()) {
                    Toast.makeText(GameSetHistoryActivity.this,
                            GameSetHistoryActivity.this.getString(R.string.msgActivateBluetooth),
                            Toast.LENGTH_LONG).show();
                }

                try {
                    // make sure at least one device was discovered
                    if (GameSetHistoryActivity.this.bluetoothHelper.getBluetoothDeviceCount() == 0) {
                        Toast.makeText(GameSetHistoryActivity.this,
                                GameSetHistoryActivity.this.getString(R.string.msgRunDiscoverDevicesFirst),
                                Toast.LENGTH_LONG).show();
                    }

                    // display devices and download
                    final String[] items = GameSetHistoryActivity.this.bluetoothHelper
                            .getBluetoothDeviceNames();

                    AlertDialog.Builder builder = new AlertDialog.Builder(GameSetHistoryActivity.this);
                    builder.setTitle(GameSetHistoryActivity.this.getString(R.string.lblSelectBluetoothDevice));
                    builder.setItems(items, new BluetoothDeviceClickListener(gameSet, items));
                    AlertDialog alert = builder.create();
                    alert.show();
                } catch (Exception e) {
                    AuditHelper.auditError(ErrorTypes.gameSetHistoryActivityError, e,
                            GameSetHistoryActivity.this);
                }
            } else if (item.itemType == Item.ItemTypes.exportToExcel) {
                try {
                    if (!AppContext.getApplication().isAppLimited()) {

                        ExportToExcelTask task = new ExportToExcelTask(GameSetHistoryActivity.this,
                                progressDialog);
                        task.setCallback(excelExportCallback);
                        task.execute(gameSet);
                    }
                } catch (Exception e) {
                    Toast.makeText(
                            GameSetHistoryActivity.this, AppContext.getApplication().getResources()
                                    .getText(R.string.msgGameSetExportError).toString() + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                    AuditHelper.auditError(ErrorTypes.excelFileStorage, e);
                }
            } else if (item.itemType == Item.ItemTypes.edit) {
                // SharedPreferences preferences =
                // PreferenceManager.getDefaultSharedPreferences(GameSetHistoryActivity.this);

                // set selected gameset as session gameset
                // AppContext.getApplication().getBizService().setGameSet(gameSet);

                // // Get non DAL stored parameters property from shared
                // preferences
                // UIHelper.fillNonComputationPreferences(gameSet.getGameSetParameters(),
                // preferences);

                // start tab gameset activity
                Intent intent = new Intent(GameSetHistoryActivity.this, TabGameSetActivity.class);
                intent.putExtra(ActivityParams.PARAM_GAMESET_ID, gameSet.getId());
                GameSetHistoryActivity.this.startActivityForResult(intent, RequestCodes.DISPLAY_WITH_FACEBOOK);
            }
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

}

From source file:com.fvd.nimbus.BrowseActivity.java

public boolean hasInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return false;
    }/*w w w.j  av  a2 s. co m*/
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    if (netInfo == null) {
        return false;
    }
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected()) {
                //Log.d(this.toString(), "test: wifi conncetion found");
                return true;
            }
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected()) {
                //Log.d(this.toString(), "test: mobile connection found");
                return true;
            }
    }
    return false;
}