編輯:關於Android編程
已經寫了幾篇關於Android源碼的,源碼代碼量太大,所以如果想分析某個模塊可能不知如何下手,說一下思路
1,分析源碼英文閱讀能力要夠,想要分析某個模塊一般找模塊對應的英文,就是模塊<!-- 允許程序獲取WiFi狀態 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"> <!--允許程序改變WiFi狀態--> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"> <!--允許程序獲取手機網絡狀態--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> <!--允許程序改變網絡狀態--> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"> <!--允許程序訪問網絡--> <uses-permission android:name="android.permission.INTERNET"></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission>
public class WifiSettings extends RestrictedSettingsFragment implements DialogInterface.OnClickListener
View view = inflater.inflate(R.layout.setup_preference, container, false);
View v = inflater.inflate(R.layout.add_preference_list_fragment,null);
mP2pSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT);
public boolean hasSystemFeature(String name) { return systemFeatureList.containsKey(name) ? systemFeatureList.get(name) : false; }
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (getResources().getBoolean(R.bool.set_wifi_priority)) { addPreferencesFromResource(R.xml.wifi_sort_settings); mDefaultTrustAP = (PreferenceCategory)findPreference("default_trust_access_points"); mConfigedAP = (PreferenceCategory)findPreference("configed_access_points"); mUnKnownAP = (PreferenceCategory)findPreference("unknown_access_points"); } else { addPreferencesFromResource(R.xml.wifi_settings); }
對於boolean值“set_wifi_priority”的值可以查看Z:\L7-A1\android\packages\apps\Settings\res\values\bools.xml文件該值表示whether to show hotspot via the ap's classification接入點優先級設定即是否按照所搜索到的WiFi優先級來顯示Wifi列表,可以看到等級有三種:默認的信任接入點,信任接入點,未知接入點因為在這裡boolean值被設置為false,所以不按優先級排序5>,接下來就是添加WiFi開關,至於通過代碼在導航欄actionbar添加switch的代碼類似藍牙,在此不再贅述,可以看到,藍牙開關switch傳入了WifiEnabler,所以對於Wifi的開關的管理位於WiFiEnablerswitch中
mWifiEnabler = new WifiEnabler(activity, actionBarSwitch);
//當WiFi狀態發生改變時會發送該廣播 mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION); // The order matters! We really should not depend on this. :( mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); //當設備網絡狀態發生改變時會發送該廣播 mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
/** * Broadcast intent action indicating that the state of establishing a connection to * an access point has changed.One extra provides the new * {@link SupplicantState}. Note that the supplicant state is Wi-Fi specific, and * is not generally the most useful thing to look at if you are just interested in * the overall state of connectivity. * @see #EXTRA_NEW_STATE * @see #EXTRA_SUPPLICANT_ERROR */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SUPPLICANT_STATE_CHANGED_ACTION = "android.net.wifi.supplicant.STATE_CHANGE";
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { //WiFi狀態生改變時去更新switch的狀態,WiFi的狀態存在於WifiManager.EXTRA_WIFI_STATE handleWifiStateChanged(intent.getIntExtra( WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)); //needPrompt方法用於判斷是否是飛行模式以及飛行模式是否無效 if (WifiSettings.needPrompt(context)) { setSwitchChecked(false); } } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) { //連接發生改變時的更新,WifiManager.EXTRA_NEW_STATE存放改變後的狀態 if (!mConnected.get()) { handleStateChanged(WifiInfo.getDetailedStateOf((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE))); } } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { //網絡狀態發生改變時的更新 NetworkInfo info = (NetworkInfo) intent.getParcelableExtra( WifiManager.EXTRA_NETWORK_INFO); mConnected.set(info.isConnected()); handleStateChanged(info.getDetailedState()); } } };
private void handleWifiStateChanged(int state) { switch (state) { case WifiManager.WIFI_STATE_ENABLING://正在打開WiFi mSwitch.setEnabled(false); break; case WifiManager.WIFI_STATE_ENABLED://WiFi已經打開 setSwitchChecked(true); mSwitch.setEnabled(true); break; case WifiManager.WIFI_STATE_DISABLING://正在關閉WiFi mSwitch.setEnabled(false); break; case WifiManager.WIFI_STATE_DISABLED://WiFi已經關閉 setSwitchChecked(false); mSwitch.setEnabled(true); break; default: setSwitchChecked(false); mSwitch.setEnabled(true); break; } }
private void handleStateChanged(@SuppressWarnings("unused") NetworkInfo.DetailedState state) { // After the refactoring from a CheckBoxPreference to a Switch, this method is useless since // there is nowhere to display a summary. // This code is kept in case a future change re-introduces an associated text. /* // WifiInfo is valid if and only if Wi-Fi is enabled. // Here we use the state of the switch as an optimization. if (state != null && mSwitch.isChecked()) { WifiInfo info = mWifiManager.getConnectionInfo(); if (info != null) { //setSummary(Summary.get(mContext, info.getSSID(), state)); } } */ }
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //Do nothing if called as a result of a state machine event if (mStateMachineEvent) { return; } if (mContext.getResources().getBoolean(R.bool.wifi_to_cell)) { ConnectivityManager mConnService = (ConnectivityManager) mContext. getSystemService(Context.CONNECTIVITY_SERVICE); if (mConnService != null) { NetworkInfo netInfo = (NetworkInfo) mConnService .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (netInfo != null && netInfo.isConnected()) { Settings.System.putInt(mContext.getContentResolver(), WIFI_IS_CONNECTED, CONNECTED); } else { Settings.System.putInt(mContext.getContentResolver(), WIFI_IS_CONNECTED, DISCONNECTED); } } } // Show toast message if Wi-Fi is not allowed in airplane mode //判斷是否在飛行模式中WiFi是不允許的 if (isChecked && (WifiSettings.needPrompt(mContext) || !WirelessSettings.isRadioAllowed( mContext, Settings.Global.RADIO_WIFI))) { Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show(); // Reset switch to off. No infinite check/listenenr loop. buttonView.setChecked(false); return; } // Disable tethering if enabling Wifi int wifiApState = mWifiManager.getWifiApState(); if (isChecked && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) || (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) { mWifiManager.setWifiApEnabled(null, false); } // shouldn't setWifiEnabled(true) in airplane mode. if (isChecked && WifiSettings.needPrompt(mContext)) { return; } else { //setWifiEnabled打開或者關閉WiFi的方法,會發送WiFi狀態改變的廣播:WIFI_STATE_CHANGED_ACTION if (mWifiManager.setWifiEnabled(isChecked)) { // Intent has been taken into account, disable until new state // is active mSwitch.setEnabled(false); } else { // Error Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show(); } } }對於mStateMachineEvent的值是在對switch進行設置時賦值,起開關保護的作用,保證當點擊switch的時候先將switch狀態設置成功再進入點擊事件方法
private void setSwitchChecked(boolean checked) { if (checked != mSwitch.isChecked()) { mStateMachineEvent = true; mSwitch.setChecked(checked); mStateMachineEvent = false; } }
一、eclipse 中生成android keystore 建立任意一個android項目(例如:AntForAndroid) Export Signed Appl
前提本教程默認以下幾點你已經完全滿足:開通了認證後的服務號 服務號開通的微信支付的認證 騰訊給你的郵件中有商戶登錄的賬號和密碼 擁有一個可供上傳代碼和設置回調域名的網站或
Contact providerContact provider是一個強大而又靈活的 Android 組件,用於管理設備上有關聯系人數據的中央存儲庫。 Contact p
記得之前京東首頁有一個效果,有一個畫軸,然後可以滾動畫軸,去打開畫(不知道怎麼去形容這個效果,就叫做畫軸效果吧- -!),然後去做相關操作,剛開始看到這個效果,想法是動態