Android 8.0 Settings往介面新增選單
阿新 • • 發佈:2018-12-27
1、SettingsActivity.java 檔案下的doUpdateTilesList方法裡新增:
setTileEnabled(new ComponentName(packageName,
Settings.WtkSecurityLockersActivity.class.getName()),
getResources().getBoolean(R.bool.show_security_lock), isAdmin);
2、Settings.java檔案下新增
public static class WtkSecurityLockersActivity extends SettingsActivity { }
3、開啟SettingsGateway.java檔案,
在ENTRY_FRAGMENTS陣列中新增:WtkSecurityLocker.class.getName(),
在SETTINGS_FOR_RESTRICTED中新增:Settings.WtkSecurityLockersActivity.class.getName(),
4、在AndroidManifest.xml中新增
<activity android:name=".Settings$WtkSecurityLockersActivity" android:taskAffinity="com.android.settings" android:label="@string/security_locker" android:icon="@drawable/ic_wtk_securitylocker" android:parentActivityName="Settings"> <intent-filter android:priority="1"> <action android:name="android.settings.WIRELESS_SETTINGS" /> <action android:name="android.settings.AIRPLANE_MODE_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> <intent-filter android:priority="11"> <action android:name="com.android.settings.action.SETTINGS"/> </intent-filter> <meta-data android:name="com.android.settings.category" android:value="com.android.settings.category.ia.homepage"/> <meta-data android:name="com.android.settings.FRAGMENT_CLASS" android:value="com.android.settings.WtkSecurityLocker"/> <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED" android:value="true" /> </activity>
5、最後在Settings下新增一個WtkSecurityLocker類,實現該類。
6、如果需要動態改變選單的Summary,需要在WtkSecurityLocker.java裡面新增:
import com.android.settings.dashboard.SummaryLoader; import android.content.Context; import android.app.Activity; private static class SummaryProvider implements SummaryLoader.SummaryProvider { private final Context mContext; private final SummaryLoader mLoader; private SummaryProvider(Context context, SummaryLoader loader) { mContext = context; mLoader = loader; } @Override public void setListening(boolean listening) { if (listening) { updateSummary(); } } private void updateSummary() { mLoader.setSummary(this, "Summary"); } } public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY = new SummaryLoader.SummaryProviderFactory() { @Override public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity, SummaryLoader summaryLoader) { return new SummaryProvider(activity, summaryLoader); } };