Settings添加1级菜单并实现跳转


android 11

方案一:

XML配置

packages/apps/Settings/res/xml/top_level_settings.xml

<Preference
        android:key="buttons_key"
        android:title="@string/buttons_title"
	    android:summary="@string/buttons_title"
        android:icon="@drawable/ic_buttons"
        android:order="-5">
	<intent android:action="android.intent.action.MAIN"
	        android:targetPackage="要跳转的包名"
	        android:targetClass="要跳转的类名" />
    </Preference>

或者

<Preference
        android:key="buttons_key"
        android:title="@string/buttons_title"
	    android:summary="@string/buttons_title"
        android:icon="@drawable/ic_buttons"
        android:order="-5">
	    android:fragment="fragment"
        settings:controller="Controller"
    </Preference>

NewPreferenceController.java

package com.android.settings.new;

import android.content.Context;
import android.os.Build;

import com.android.settings.core.BasePreferenceController;

public class StatusPreferenceController extends BasePreferenceController {

    public StatusPreferenceController(Context context, String key) {
        super(context, key);
    }

    @Override
    public int getAvailabilityStatus() {
    //控制菜单是否显示
        return AVAILABLE;
    }

    @Override
    public CharSequence getSummary() {
       return "Build.VERSION.RELEASE";
    }
}

NewFragment.java

package  com.android.settings.new;

import android.app.settings.SettingsEnums;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
public class NewFragment extends DashboardFragment {

    @Override
protected int getPreferenceScreenResId() {
//添加子菜单布局,就是你要加载的那个xml,点击菜单后显示的内容
        return R.xml.new_xml;
}

    @Override
    protected String getLogTag() {
        return "NewFragment_LOG";
    }

    @Override
    public int getMetricsCategory() {
        return 0;//这里切记不能<0否则子菜单中配置Fragment时候会报错
    }
}

SettingsGateway.java需要的修改

packages/apps/Settings/src/com/android/settings/core/gateway/SettingsGateway.java

/*................*/
+import com.android.settings.new.NewFragment;
/*................*/
 
public static final String[] SETTINGS_FOR_RESTRICTED = {
@@ -375,6 +378,9 @@
              ............
             Settings.MyDeviceInfoActivity.class.getName(),
             Settings.ModuleLicensesActivity.class.getName(),
             UserBackupSettingsActivity.class.getName(),
+			NewFragment.class.getName(),
             ............
             }

方案二:

SettingsActivity.java需要的修改

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


import android.content.pm.PackageInfo;
import com.android.settings.remote.SimRemoteActivity;
import com.android.settings.remote.SosEmergencyActivity;
import com.android.settings.remote.SpeedDialActivity;

public class SettingsActivity extends SettingsBaseActivity
        implements PreferenceManager.OnPreferenceTreeClickListener,
        PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
        ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
      ...............................
       import java.util.ArrayList;
 import java.util.List;
 
+import android.content.pm.PackageInfo;
+import com.android.settings.remote.SimRemoteActivity;
+import com.android.settings.remote.SosEmergencyActivity;
+import com.android.settings.remote.SpeedDialActivity;
 
 public class SettingsActivity extends SettingsBaseActivity
         implements PreferenceManager.OnPreferenceTreeClickListener,
@@ -627,6 +632,18 @@
         // SettingsBaseActivity will pick up on the updates automatically.
         AsyncTask.execute(() -> doUpdateTilesList());
     }
+		private boolean isApkExist(String packageName){
+		PackageManager pm = this.getPackageManager();
+		PackageInfo pInfo = null;
+		try{
+			pInfo = pm.getPackageInfo(packageName,PackageManager.GET_ACTIVITIES);
+		}catch(PackageManager.NameNotFoundException e){
+			return false;
+		}catch(Exception xe){
+			return false;
+		}
+		return true;
+	}
 
     private void doUpdateTilesList() {
         PackageManager pm = getPackageManager();
@@ -690,6 +707,20 @@
                         Settings.SmartControlsSettingsActivity.class.getName()),
                 SmartControlsSettings.isSupportSmartControl(this), isAdmin)
                 || somethingChanged;
+		somethingChanged = setTileEnabled(changedList,new ComponentName(packageName,
+                        SimRemoteActivity.class.getName()),
+                isApkExist("要跳转界面包名"), isAdmin)
+                || somethingChanged;
         boolean supportDynaNaviBar = getResources().getBoolean(com.android.internal.R.bool.config_support_dynamic_navigation_bar);

AndroidManifest.xml需要的修改

packages/apps/Settings/AndroidManifest.xml

             android:excludeFromRecents="true"
             android:theme="@android:style/Theme.Material.Light.Dialog" >
         </activity>
+	        <activity android:name="com.android.settings.remote.SimRemoteActivity"
+                  android:label="@string/sim_remote" 显示的标题
+                  android:icon="@drawable/ic_settings_sim"> 显示在最前面得图标
+            <intent-filter android:priority="7">  在设置中显示的顺序
+                <action android:name="com.android.settings.action.SETTINGS"/>  标识显示在1级菜单的属性
+            </intent-filter>
+            <meta-data android:name="com.android.settings.category"
+                       android:value="com.android.settings.category.ia.homepage"/>
+        </activity>
      </application>
 </manifest>  

新增SimRemoteActivity.java需要的修改

packages\apps\Settings\src\com\android\settings\remote\SimRemoteActivity.java

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.remote;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.content.ComponentName;

import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.search.SearchIndexableRaw;

import java.util.ArrayList;
import java.util.List;

/**
 * Trampoline activity that decides which version of support should be shown to the user.
 */
public class SimRemoteActivity extends Activity implements Indexable {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		Intent mIntent=new Intent();
		ComponentName mComponentName=new ComponentName("要跳转得包名", "要跳转得类名");
		mIntent.setComponent(mComponentName);
		startActivity(mIntent);
        finish();
    }

    /**
     * For Search.
     */
    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider() {
                //private static final String SUPPORT_SEARCH_INDEX_KEY = "support_dashboard_activity";

                @Override
                public List<SearchIndexableRaw> getRawDataToIndex(Context context,
                        boolean enabled) {

                    final List<SearchIndexableRaw> result = new ArrayList<>();

                    // Add the activity title
                    SearchIndexableRaw data = new SearchIndexableRaw(context);
                    data.title = context.getString(R.string.sim_remote);
                    data.screenTitle = context.getString(R.string.settings_label);
                    data.iconResId = R.drawable.ic_settings_sim;
                    data.intentTargetPackage = "要跳装的包名";
                    data.intentTargetClass = "要跳装的类名";
                    data.intentAction = Intent.ACTION_MAIN;
                    result.add(data);

                    return result;
                }
            };
}

SettingsGateway.java需要的修改

packages/apps/Settings/src/com/android/settings/core/gateway/SettingsGateway.java

/*................*/
+import com.android.settings.remote.SimRemoteActivity;
/*................*/
 
public static final String[] SETTINGS_FOR_RESTRICTED = {
@@ -375,6 +378,9 @@
              ............
             Settings.MyDeviceInfoActivity.class.getName(),
             Settings.ModuleLicensesActivity.class.getName(),
             UserBackupSettingsActivity.class.getName(),
+			SimRemoteActivity.class.getName(),
             ............
             }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值