Android P系统中开启NFC时,系统的状态栏中没有NFC图标,加强用户使用体验,故在SystemUI状态栏中添加NFC图标。该修改主要基于NFC开启的广播,通过接受广播,获取NFC的状态值,依据NFC的状态值显示或者隐藏NFC图标。
1.添加对应的图标,建议使用SVG。
frameworks/base/packages/SystemUI/res/drawable/stat_sys_nfc.xml
<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:height="17dp"
+ android:width="17dp"
+ android:viewportHeight="1024"
+ android:viewportWidth="1024" >
+ <path android:fillColor="#FF000000"
+ android:pathData="M624,444.44v149.33L312.89,307.56v448C312.89,904.43 424.89,967.11 424.89,967.11h-99.56c-66.08,-24.87 -108.71,-122.24 -112,-199.11L213.33,71.11l410.67,373.33zM400,593.78v-149.33L711.11,730.67v-448C711.11,133.87 599.11,71.11 599.11,71.11h99.56c66.08,24.87 108.71,122.24 112,199.11v696.89L400,593.78z"/>
+</vector>
2.添加对应的广播监听
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
+ filter.addAction(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
case AudioManager.ACTION_HEADSET_PLUG:
updateHeadsetPlug(intent);
break;
case NfcAdapter.ACTION_ADAPTER_STATE_CHANGED:
updateNfc(intent);
break;
3.添加对应状态处理
private final void updateNfc(Intent intent) {
final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_OFF);
Log.v(TAG, "Nfc state = "+ state);
switch (state) {
case NfcAdapter.STATE_OFF:
mIconController.setIconVisibility(mSlotNfc, false);
break;
case NfcAdapter.STATE_TURNING_OFF:
break;
case NfcAdapter.STATE_ON:
mIconController.setIcon(mSlotNfc, R.drawable.stat_sys_nfc,
mContext.getString(R.string.accessibility_quick_settings_nfc_on));
mIconController.setIconVisibility(mSlotNfc, true);
break;
case NfcAdapter.STATE_TURNING_ON:
break;
}
}
4.参考其他类似图标更新(耳机),添加对应的字符资源
frameworks/base/packages/SystemUI/res/values/strings.xml
<string name="accessibility_quick_settings_nfc">NFC.</string>
<!-- Content description of the bluetooth tile in quick settings when off (not shown on the screen). [CHAR LIMIT=NONE] -->
<string name="accessibility_quick_settings_nfc_off">NFC off.</string>
<!-- Content description of the bluetooth tile in quick settings when on (not shown on the screen). [CHAR LIMIT=NONE] -->
<string name="accessibility_quick_settings_nfc_on">NFC on.</string>