本文主要是介绍Android V Settings应用启动APN Settings界面的代码逻辑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
逻辑说明
1、manifest清单文件配置<activity>(如ApnSettingsActivity),通过配置内部元元素<meta-data>元数据绑定界面对应的java类(如下案例绑定Fragement是ApnSettings类)
- 静态内部类在AndroidManifest.xml文件中通过<meta-data>将相应的Fragment绑定起来。
<activity android:name="Settings$ApnSettingsActivity" <meta-data android:name="com.android.settings.FRAGMENT_CLASS"android:value="com.android.settings.network.apn.ApnSettings" />
2、ApnSettings(Fragment)是界面的具体逻辑实现
说明:
- Settings类中定义各子静态xxxSettingsActivity类
- Settings类和各子xxxSettingsActivity都继承自SettingsActivity类
- Settings与SubSettings中基本是空Activity,即Activity没有重写任意7大生命周期方法 (/*empty*/)
代码案例
Settings应用
AndroidManifest.xml
<meta-data>
元素可用于在AndroidManifest.xml文件中为应用程序、组件或权限声明添加一些附加信息。
<intent-filter>
元素用来描述组件(如活动、服务或接收器)能够响应的Intent的一部分。它通常用于在清单文件中声明组件所需的Intent过滤器。1、其中包含了
<action>
和<category>
元素。<action>
元素指定了组件能够处理的Intent动作,而<category>
元素用于进一步限制组件的响应条件。2、也可以在其中包含
android:priority
属性,用来指定组件处理对应Intent的优先级,数字越大优先级越高。在Java中,用
$
符号来表示嵌套类和内部类的关系。所以这个声明指明了ApnSettingsActivity
是Settings
类中的一个内部类。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"coreApp="true"android:sharedUserId="android.uid.system"> <!--表明应用运行在system进程--><original-package android:name="com.android.settings"/><!--应用配置--><applicationandroid:name=".SettingsApplication"android:label="@string/settings_label"android:icon="@drawable/ic_launcher_settings"android:theme="@style/Theme.Settings"android:hardwareAccelerated="true"android:requiredForAllUsers="true"android:supportsRtl="true"android:backupAgent="com.android.settings.backup.SettingsBackupHelper"android:restoreAnyVersion="true"android:usesCleartextTraffic="true"android:defaultToDeviceProtectedStorage="true"android:directBootAware="true"android:appComponentFactory="androidx.core.app.CoreComponentFactory"android:gwpAsanMode="always"android:enableOnBackInvokedCallback="true"tools:replace="android:label"><!--Activity属性--><!--指明ApnSettingsActivity是Settings类中的一个内部类。--><activity android:name="Settings$ApnSettingsActivity"android:label="@string/apn_settings"android:exported="true"android:configChanges="orientation|keyboardHidden|screenSize"><intent-filter android:priority="1"><action android:name="android.settings.APN_SETTINGS" /><category android:name="android.intent.category.DEFAULT" /></intent-filter><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.DEFAULT" /><category android:name="android.intent.category.VOICE_LAUNCH" /></intent-filter><!--绑定界面ApnSettings--><meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"android:value="true" /><meta-data android:name="com.android.settings.FRAGMENT_CLASS"android:value="com.android.settings.network.apn.ApnSettings" /><meta-data android:name="com.android.settings.HIGHLIGHT_MENU_KEY"android:value="@string/menu_key_network"/></activity></application>
</manifest>
Settings.java
结合清单文件配置的Settings$ApnSettingsActivity,说明ApnSettingsActivity是Settings类中的一个内部类,但没有具体的Activity实现,界面实现看meta-data绑定的Fragment。
package com.android.settings;/*** Top-level Settings activity*/
public class Settings extends SettingsActivity {/** Settings subclasses for launching independently.*/public static class ApnSettingsActivity extends SettingsActivity { /* empty */ }}
移动网络
mobile_network_settings.xml 布局文件
Settings\app\src\main\res\xml\mobile_network_settings.xml
- 用户界面路径:Settings -> Network&Internet -> SIMs -> xxx(运营商名称)。
- APN菜单对应“telephony_apn_key”。
<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"xmlns:settings="http://schemas.android.com/apk/res-auto"android:key="mobile_network_pref_screen"><PreferenceCategoryandroid:key="enabled_state_container"android:title="@string/summary_placeholder"settings:controller="com.android.settings.network.telephony.DisabledSubscriptionController"android:layout="@layout/preference_category_no_label"><!--网络模式--><ListPreferenceandroid:key="preferred_network_mode_key"android:title="@string/preferred_network_mode_title"android:summary="@string/preferred_network_mode_summary"android:entries="@array/preferred_network_mode_choices"android:entryValues="@array/preferred_network_mode_values"android:dialogTitle="@string/preferred_network_mode_dialogtitle"settings:controller="com.android.settings.network.telephony.PreferredNetworkModePreferenceController"/><!--选网(自动/手动)--><PreferenceCategoryandroid:key="network_operators_category_key"android:title="@string/network_operator_category"settings:allowDividerBelow="true"settings:controller="com.android.settings.network.telephony.NetworkPreferenceCategoryController"><com.android.settings.spa.preference.ComposePreferenceandroid:key="auto_select_key"android:title="@string/select_automatically"settings:controller="com.android.settings.network.telephony.gsm.AutoSelectPreferenceController"/><Preferenceandroid:key="choose_network_key"android:title="@string/choose_network_title"settings:controller="com.android.settings.network.telephony.gsm.OpenNetworkSelectPagePreferenceController"/></PreferenceCategory><!--APN入口--><!--We want separate APN setting from reset of settings because we want user to change it with caution--><com.android.settingslib.RestrictedPreferenceandroid:key="telephony_apn_key"android:persistent="false"android:title="@string/mobile_network_apn_title"settings:keywords="@string/keywords_access_point_names"settings:controller="com.android.settings.network.telephony.ApnPreferenceController"/></PreferenceCategory></PreferenceScreen>
该界面可以disable/enable 卡,对应首行“Use this SIM”开关。
<string name="mobile_network_use_sim_on">Use this SIM</string>
MobileNetworkSettings.java
Settings\app\src\java\com\android\settings\network\telephony\MobileNetworkSettings.java
package com.android.settings.network.telephony;@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class MobileNetworkSettings extends AbstractMobileNetworkSettings implementsMobileNetworkRepository.MobileNetworkCallback {public MobileNetworkSettings() {super(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);}//Fragement生命周期之一,用于将Fragment与Activity进行关联。@Overridepublic void onAttach(Context context) {super.onAttach(context);//初始化注册信息if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {Log.d(LOG_TAG, "Invalid subId, get the default subscription to show.");SubscriptionInfo info = SubscriptionUtil.getSubscriptionOrDefault(context, mSubId);if (info == null) {Log.d(LOG_TAG, "Invalid subId request " + mSubId);return;}mSubId = info.getSubscriptionId();Log.d(LOG_TAG, "Show NetworkSettings fragment for subId" + mSubId);}Intent intent = getIntent();if (intent != null) {int updateSubscriptionIndex = intent.getIntExtra(Settings.EXTRA_SUB_ID,SubscriptionManager.INVALID_SUBSCRIPTION_ID);//..omit}//传入intent值,对应的卡use(ApnPreferenceController.class).init(mSubId);}
onAttach()
方法是Fragment
的生命周期方法之一,用于将Fragment
与Activity
进行关联。
APN设置
ApnPreferenceController.java
Settings\app\src\main\java\com\android\settings\network\telephony\ApnPreferenceController.java
- 插件化:可以在此处初始化的时候安装插件
- 【疑问】为什么APN Settings 注释是跑在phone进程的??
package com.android.settings.network.telephony;/*** Preference controller for "Apn settings"*/
public class ApnPreferenceController extends TelephonyBasePreferenceController implementsLifecycleObserver, OnStart, OnStop {@VisibleForTestingCarrierConfigCache mCarrierConfigCache;private Preference mPreference;private DpcApnEnforcedObserver mDpcApnEnforcedObserver;public ApnPreferenceController(Context context, String key) {super(context, key);mCarrierConfigCache = CarrierConfigCache.getInstance(context);mDpcApnEnforcedObserver = new DpcApnEnforcedObserver(new Handler(Looper.getMainLooper()));}@Overridepublic int getAvailabilityStatus(int subId) {final PersistableBundle carrierConfig = mCarrierConfigCache.getConfigForSubId(subId);final boolean isCdmaApn = MobileNetworkUtils.isCdmaOptions(mContext, subId)&& carrierConfig != null&& carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL);final boolean isGsmApn = MobileNetworkUtils.isGsmOptions(mContext, subId)&& carrierConfig != null&& carrierConfig.getBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL);final boolean hideCarrierNetwork = carrierConfig == null|| carrierConfig.getBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL);return !hideCarrierNetwork && (isCdmaApn || isGsmApn)? AVAILABLE: CONDITIONALLY_UNAVAILABLE;}@Overridepublic void onStart() {mDpcApnEnforcedObserver.register(mContext);}@Overridepublic void onStop() {mDpcApnEnforcedObserver.unRegister(mContext);}//根据APN key显示 preference@Overridepublic void displayPreference(PreferenceScreen screen) {super.displayPreference(screen);mPreference = screen.findPreference(getPreferenceKey());}@Overridepublic void updateState(Preference preference) {super.updateState(preference);if (mPreference == null) {return;}((RestrictedPreference) mPreference).setDisabledByAdmin(MobileNetworkUtils.isDpcApnEnforced(mContext)? RestrictedLockUtilsInternal.getDeviceOwner(mContext): null);}//点击跳转到APN Settings@Overridepublic boolean handlePreferenceTreeClick(Preference preference) {if (getPreferenceKey().equals(preference.getKey())) {// This activity runs in phone process, we must use intent to startfinal Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);//为什么指示运行在Phone进程?//intent.setPackage(mContext.getPackageName()); //源码没有这句// This will setup the Home and Search affordanceintent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true);intent.putExtra(ApnSettings.SUB_ID, mSubId);mContext.startActivity(intent);return true;}return false;}//从MobileNetworkSettings传入的值public void init(int subId) {mSubId = subId;}}
DPC 是 Android Device Policy Controller ?
ApnSettings.java 界面逻辑
package com.android.settings.network.apn;public class ApnSettings extends RestrictedSettingsFragmentimplements Preference.OnPreferenceChangeListener {public ApnSettings() {super(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);}@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);//初始化全局变量,subId、phoneId、TelephonyManager相关及业务监听和Handler处理}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);getEmptyTextView().setText(R.string.apn_settings_not_available);if (mUnavailable) {addPreferencesFromResource(R.xml.placeholder_prefs);return;}addPreferencesFromResource(R.xml.apn_settings);}@Overridepublic void onResume() {super.onResume();}@Overridepublic void onPause() {super.onPause();}@Overridepublic void onDestroy() {super.onDestroy();//取消各种监听,unregisterXxxxx//子线程关闭,mApnThread.quit();}@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {//行点击触发,ApnEditor编译页面}//可以自定义覆写菜单显示逻辑,执行于onCreateOptionsMenu之前//解决加载菜单项异常问题,常见于定制导致的时序问题。@Overridepublic void onPrepareOptionsMenu(Menu menu) {//available判断逻辑和菜单功能定制super.onPrepareOptionsMenu(menu);}@Overridepublic void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {//客制化逻辑super.onCreateOptionsMenu(menu, inflater);}//菜单点击事件处理@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case MENU_NEW: //新增APNaddNewApn();return true;case MENU_RESTORE: //重置APNrestoreDefaultApn();return true;}return super.onOptionsItemSelected(item);}
在Android中,
onCreate()
方法是在Activity
创建时首先被调用的。这个方法用于进行一些初始化操作,比如设置布局、绑定控件以及准备数据等。而
onActivityCreated()
方法则是Fragment
中的生命周期方法,在Activity
的onCreate()
方法执行完毕之后,Fragment
相关的生命周期方法才会被调用。所以onActivityCreated()
方法会在Activity
的onCreate()
方法执行完毕之后才会被调用。总结起来,
onCreate()
方法先于onActivityCreated()
方法被执行。
这篇关于Android V Settings应用启动APN Settings界面的代码逻辑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!