20.android 7.0,8.0,9.0 Settings设置内置选项在一级菜单activity方式

我的私人博客:www.mrloveqin.top 可以查看更多内容

20.Settings内置选项在一级菜单activity方式
① 在AndroidManifest.xml 添加如下代码

<activity android:name=".HardKey"
                    android:label="Mrlove"
                    android:icon="@drawable/ic_home_wikofeatures"
                    android:taskAffinity="">
                <intent-filter android:priority="1">
                    <action android:name="com.android.settings.HARDKEY" />
                    <action android:name="android.settings.HARDKEY" />
                    <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" />
                    <category android:name="com.android.settings.SHORTCUT" />
                </intent-filter>
		<!--在category中的物理位置(按优先级排序时用到,并不是指第9个位置,数越大优先级越大越靠前)-->
                <intent-filter android:priority="9">
                    <action android:name="com.android.settings.action.SETTINGS" />
                </intent-filter>
		<!--此项在主setting位置(ia.homepage)-->
                <meta-data android:name="com.android.settings.category"
                    android:value="com.android.settings.category.ia.homepage" />

                <meta-data android:name="com.android.settings.ACTIVITY_ACTION"
                    android:value="com.android.settings.HardKey" />
                
                <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
                    android:value="true" />
            </activity>

② 实现HardKey这个activity代码如下

package com.android.settings;
import android.app.Activity;
import android.os.Bundle;

public class HardKey extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hardkey);
    }
}

③ 创建hardkey.xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="hello world" />
</LinearLayout>

你可能感兴趣的:(Android)