Android 5.1 open data flow data opening process-Alibaba Cloud Developer Community

private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mBinding) return;
 
            final boolean dataEnabled = !mDataEnabled.isChecked();
            final String currentTab = mCurrentTab;
            if (TAB_MOBILE.equals(currentTab) || currentTab.startsWith(TAB_SIM)) {
                if (dataEnabled) {
                    setMobileDataEnabled(true);
                    if (mPolicyEditor.getPolicyWarningBytes(mTemplate) == WARNING_DISABLED) {
                        mPolicyEditor.setPolicyWarningBytes(mTemplate, 2 * GB_IN_BYTES);
                    }
                } else {
                    // disabling data; show confirmation dialog which eventually
                    // calls setMobileDataEnabled() once user confirms.
                    ConfirmDataDisableFragment.show(DataUsageSummary.this);
                }
            }
 
            updatePolicy(false);
        }
    };
The following section obtains the current obtained SubId based on the phoneId
mTelephonyManager . setDataEnabled ( subId [ 0 ], enabled );
   private void setMobileDataEnabled(boolean enabled) {
        if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
        // How about exposing sub based API like TelephonyManager.setDataEnabled(int subId);
        if (mCurrentTab.startsWith(TAB_SIM)) {
            int phoneId = multiSimGetCurrentSub();
 
            // as per phone, set the individual flag
            android.provider.Settings.Global.putInt(getActivity().getContentResolver(),
                    android.provider.Settings.Global.MOBILE_DATA + phoneId, enabled ? 1 : 0);
 
            int[] subId = SubscriptionManager.getSubId(phoneId);
            mTelephonyManager.setDataEnabled(subId[0], enabled);
        } else {
            mTelephonyManager.setDataEnabled(enabled);
            mMobileDataEnabled = enabled;
        }
        updatePolicy(false);
    }
frameworks/base
the data switch is called to TelephonyManager.setDataEnabled(), whether it is from traffic usage or from the drop-down "Quick Settings" (Quick Setting) (will be added later)
android/frameworks/base/telephony/java/android/telephony/TelephonyManager.java
getITelephony (). setDataEnabled ( subId , enable );
 /** @hide */
    @SystemApi
    public void setDataEnabled(int subId, boolean enable) {
        try {
            AppOpsManager appOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE);
            if (enable) {
                if (appOps.noteOp(AppOpsManager.OP_DATA_CONNECT_CHANGE) != AppOpsManager.MODE_ALLOWED) {
                    Log.w(TAG, "Permission denied by user.");
                    return;
                }
            }
            Log.d(TAG, "setDataEnabled: enabled=" + enable);
            getITelephony().setDataEnabled(subId, enable);
        } catch (RemoteException e) {
            Log.e(TAG, "Error calling setDataEnabled", e);
        }
    }
Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now