android NetworkSetting网络模式菜单开关

本文的目的是因为,我们之前做了针对不同的运营商设置不同的网络菜单,那么这样一来,如果是需要调试或者测试时,网络模式就没办法显示所有菜单,并且也无法修改。

android N以前selinux策略并未限制priv-app的SystemProperities.set()的使用,我们可以直接在Dialer上添加数字暗码,设置不同的persist属性值,以便来控制菜单是显示所有还是保持设置好的运营商列表.

那么android N上怎么办呢,虽然Dialer无法直接去干这个set persist属性的活,那就想想其他办法,于是,我们找到了设置Settings.
先说一下大概的想法就是,
1.在设置里新添加继承SettingsPreferenceFragment 的独立Fragment,主要的控件就是一个SwitchPreference的开关,
开关与否控制persist属性值;
2.当然,菜单又要对用户隐藏,最后还是要借用Dialer暗码启动;整个流程就是如此.

以上基本上就是在setting中新添模块的必经之路
具体实现:
1. AndroidMainfest.xml中注册
    <!--ShowAllNetworkModeSettings setting-->
    <activity android:name="Settings$ShowAllNetworkModeSettingsActivity"
      android:label="@string/open_all_network_mode_title"
      android:taskAffinity=""
      android:parentActivityName="Settings">
      <intent-filter android:priority="1">
                <action android:name="com.android.settings.action.SETTINGS" />
      </intent-filter>
      <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
            <intent-filter android:priority="10">
                <action android:name="com.android.settings.action.SETTINGS" />
            </intent-filter>
      <intent-filter>
          <action android:name="com.android.settings.show.network.mode.SHOW_ALL_NETWORK" />
          <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
            <!--------meta-data android:name="com.android.settings.category"
                android:value="com.android.settings.category.device" /------->
            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                android:value="com.android.settings.ShowAllNetworkModeSettings" />
            <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
                android:value="true" />
    <!--ShowAllNetworkModeSettings setting-->     

以上manifest注释的部分实现隐藏当前Fragment,以至于在设置中不可见.

下面是新增Fragment的java代码

packages\apps\Settings\src\com\android\settings\ShowAllNetworkModeSettings.java

package com.android.settings;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemProperties;
import com.android.settings.SettingsPreferenceFragment;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;

import android.util.Log;


public class ShowAllNetworkModeSettings extends SettingsPreferenceFragment implements
	Preference.OnPreferenceClickListener {
    
    

    private static final String KEY_SHOW_ALL_NETWORK_MODE = "show_all_network_mode";
    private SwitchPreference mShowNetworkMode;

    public void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.show_network_mode);
        mShowNetworkMode = (SwitchPreference) findPreference(KEY_SHOW_ALL_NETWORK_MODE);
        mShowNetworkMode.setOnPreferenceClickListener(this);

    }

    public void onResume() {
    
    
        super.onResume();
        mShowNetworkMode.setChecked(SystemProperties.get("persist.sys.show.allnwmode", "no").equals("yes"));

    }

    public boolean onPreferenceClick(Preference preference) {
    
    
        if(preference == mShowNetworkMode) {
    
    
           SystemProperties.set("persist.sys.show.allnwmode", mShowNetworkMode.isChecked()? "yes":"no");

        }
        return false;

    }
    @Override
    protected int getMetricsCategory() {
    
    
        return 499;//replace id by number
    }

}

UI添加
在这里插入图片描述

添加Fragment必须要满足的继承关系
在这里插入图片描述
最后Dialer添加暗码,Intent启动activity
在这里插入图片描述
其实整个过程就是在设置中新添加一个新Fragment菜单,但是这个是特殊的需要被隐藏的菜单。

以上记录添加过程.

猜你喜欢

转载自blog.csdn.net/jeephao/article/details/103788256