Android development: solution to sleep and wake up or cmwap/cmnet network connection failure after startup

Source: Internet
Author: User

The Android mobile phone (Mobile GSM) cannot successfully enable the network link after sleep or boot (the settings are normal). Sometimes, even the status bar icon is connected, but the network is still unavailable.

The following solution is not available for general use, but can be used to test the mobile version of HTC wild fire mobile phone:

(This problem is hard to solve. We can see from the related articles on this site that we failed to switch over the APN. It is estimated that the Network enable = false)

import java.lang.reflect.Method;004 005import android.content.ContentResolver;006import android.content.ContentValues;007import android.content.Context;008import android.database.Cursor;009import android.database.SQLException;010import android.net.ConnectivityManager;011import android.net.NetworkInfo;012import android.net.Uri;013import android.telephony.TelephonyManager;014import android.util.Log;015/**016 * Wizzer.cn017 * @author Wizzer018 *019 */020public class NetCheck {021    public static final Uri APN_URI = Uri.parse("content://telephony/carriers");022    public static final Uri DEFAULTAPN_URI = Uri023            .parse("content://telephony/carriers/restore");024    public static final Uri CURRENT_APN_URI = Uri025            .parse("content://telephony/carriers/preferapn");026    public static Context c1;027 028    public static String getCurrentAPNFromSetting(ContentResolver resolver) {029        Cursor cursor = null;030        try {031            cursor = resolver.query(CURRENT_APN_URI, null, null, null, null);032            String curApnId = null;033            String apnName1 = null;034            if (cursor != null && cursor.moveToFirst()) {035                curApnId = cursor.getString(cursor.getColumnIndex("_id"));036                apnName1 = cursor.getString(cursor.getColumnIndex("apn"));037            }038            Log.e("NetCheck getCurrentAPNFromSetting", "curApnId:" + curApnId039                    + " apnName1:" + apnName1);040            // find apn name from apn list041            if (curApnId != null) {042                cursor = resolver.query(APN_URI, null, " _id = ?",043                        new String[] { curApnId }, null);044                if (cursor != null && cursor.moveToFirst()) {045                    String apnName = cursor.getString(cursor046                            .getColumnIndex("apn"));047                    return apnName;048                }049            }050 051        } catch (SQLException e) {052            Log.e("NetCheck getCurrentAPNFromSetting", e.getMessage());053        } finally {054            if (cursor != null) {055                cursor.close();056            }057        }058 059        return null;060    }061 062    public static int updateCurrentAPN(ContentResolver resolver, String newAPN) {063        Cursor cursor = null;064        try {065            // get new apn id from list066            cursor = resolver.query(APN_URI, null, " apn = ? and current = 1",067                    new String[] { newAPN.toLowerCase() }, null);068            String apnId = null;069            if (cursor != null && cursor.moveToFirst()) {070                apnId = cursor.getString(cursor.getColumnIndex("_id"));071            }072            Log.e("NetCheck updateCurrentAPN", "apnId:" + apnId);073            // set new apn id as chosen one074            if (apnId != null) {075                ContentValues values = new ContentValues();076                values.put("apn_id", apnId);077                resolver.update(CURRENT_APN_URI, values, null, null);078            } else {079                // apn id not found, return 0.080                return 0;081            }082        } catch (SQLException e) {083            Log.e("NetCheck updateCurrentAPN", e.getMessage());084        } finally {085            if (cursor != null) {086                cursor.close();087            }088        }089 090        // update success091        return 1;092    }093 094    public String getApn(Context c) {095        boolean netSataus = false;096 097        ConnectivityManager conManager = (ConnectivityManager) c098                .getSystemService(Context.CONNECTIVITY_SERVICE);099        if (conManager.getActiveNetworkInfo() != null) {100            netSataus = conManager.getActiveNetworkInfo().isAvailable();101 102        }103        NetworkInfo info = conManager104        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);105        String oldAPN = StringUtils.null2String(info.getExtraInfo());106        Log107        .e("NetCheck getApn", "oldAPN:" + oldAPN + " netSataus:"108                + netSataus);109        if (netSataus == false) {110            Log.e("NetCheck getApn", "setMobileDataEnabled(true)");111            setMobileDataEnabled(c, true); 112 113            try {114                Thread.sleep(4012);115            } catch (InterruptedException e) {116                e.printStackTrace();117            }118        }119        if("".equals(oldAPN)){120            updateCurrentAPN(c.getContentResolver(), "cmnet");121            try {122                Thread.sleep(1500);123            } catch (InterruptedException e) {124                e.printStackTrace();125            }126        }127        info = conManager128        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);129        oldAPN = StringUtils.null2String(info.getExtraInfo());130        Log131                .e("NetCheck getApn", "newApn:" + oldAPN);132        return oldAPN.toLowerCase();133    }134 135    public boolean setMobileDataEnabled(Context c, boolean enabled) {136        final TelephonyManager mTelManager;137        mTelManager = (TelephonyManager) c138                .getSystemService(Context.TELEPHONY_SERVICE);139        try {140 141            Method m = mTelManager.getClass()142                    .getDeclaredMethod("getITelephony");143            m.setAccessible(true);144            Object telephony = m.invoke(mTelManager);145            m = telephony.getClass().getMethod(146                    (enabled ? "enable" : "disable") + "DataConnectivity");147            m.invoke(telephony);148            return true;149        } catch (Exception e) {150            Log.e("NetCheck ", "cannot fake telephony", e);151            return false;152        }153    }154 155}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.