Android 9/10 Launcher3 适配出现的问题修改

Android 10 将桌面设置图标长按出现的弹框中的电池去掉,去掉后如图:
在这里插入图片描述
这个主要是关于Android新功能Shortcuts的运用。
Launcher3的流程:
在packages\apps\Launcher3\src\com\android\launcher3\views\OptionsPopupView.java
找到布局packages\apps\Launcher3\res\layout\longpress_options_menu.xml
加载packages\apps\Launcher3\src\com\android\launcher3\shortcuts\DeepShortcutView.java、
packages\apps\Launcher3\res\layout\deep_shortcut.xml

在Launcher只是一个布局的加载,数据是在Settings.apk控制提供,

packages\apps\Settings\src\com\android\settings\shortcut\CreateShortcut.java
packages\apps\Settings\res\xml\create_shortcut.xml
packages\apps\Settings\src\com\android\settings\shortcut\CreateShortcutPreferenceController.java
在上面的三个文件是一个加载流程逻辑。
关键是这里
packages\apps\Settings\AndroidManifest.xml

<!-- Alias for launcher activity only, as this belongs to each profile. -->
        <activity-alias android:name="Settings"
                android:label="@string/settings_label_launcher"
                android:launchMode="singleTask"
                android:targetActivity=".homepage.SettingsHomepageActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            **<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>**
        </activity-alias>

找到这个文件
packages\apps\Settings\res\xml\shortcuts.xml
更改即可

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
    <shortcut
        android:shortcutId="manifest-shortcut-wifi"
        android:icon="@drawable/ic_shortcut_wireless"
        android:shortcutShortLabel="@string/wifi_settings" >
        <intent android:action="android.settings.WIFI_SETTINGS" />
    </shortcut>
    <shortcut
        android:shortcutId="manifest-shortcut-data-usage"
        android:icon="@drawable/ic_shortcut_data_usage"
        android:shortcutShortLabel="@string/data_usage_summary_title">
        <intent
            android:action="android.intent.action.MAIN"
            android:targetPackage="com.android.settings"
            android:targetClass="com.android.settings.Settings$DataUsageSummaryActivity" />
    </shortcut>
<!--    <shortcut
        android:shortcutId="manifest-shortcut-battery"
        android:icon="@drawable/ic_shortcut_battery"
        android:shortcutShortLabel="@string/power_usage_summary_title" >
        <intent android:action="android.intent.action.POWER_USAGE_SUMMARY" />
    </shortcut>
	-->
</shortcuts>

参考文章:https://my.oschina.net/shenhuniurou/blog/877148

Android7.1的Shortcuts也出来好久了,但是没有在自己的应用上尝试过,原因是之前手机一直是6.0的系统,直到最近才刷到了7.1.1,不过我发现,除了Google公司自己的app,适配了shortcuts功能的确实很少,可能是Android N的系统使用普及度太小,于是果断决定给我们公司的app加上shortcuts的功能,虽然是一些不起眼的功能,但至少要走在技术革新的最前沿吧。
在这里插入图片描述

shortcuts
使用及说明

关于Shortcuts的官方文档在这里App Shortcuts

Shortcuts的作用:官方的说法,让用户在你的app上快速开始一个常用或者推荐的任务。

使用条件:targetSdkVersion必须在25或者更高。

类型:静态(xml方式)和动态(代码方式)
注意事项:虽然其他app是不能在你app的shortcuts里访问元数据,但是启动器本身却可以访问这些数据。因此,我们应该在元数据中隐藏敏感的用户信息。

静态Shortcuts

之所以称为静态,是因为它是定义在资源文件中的,除非更新整个app,否则是不能更改shortcuts的东西,不过这种方式使用起来也跟简单,两步搞定。

第一步在AndroidManifest.xml中的启动activity中加上meta-data标签:其中name是固定的,resource是一个名为shortcuts.xml的资源文件。

<activity
	android:name=".ui.other.welcome.SplashActivity"
	android:configChanges="orientation|keyboardHidden|screenSize"
	android:label="@string/app_name"
	android:launchMode="singleTop"
	android:screenOrientation="portrait"
	android:theme="@style/AppTheme.NoActionBar">
	<intent-filter>
		<action android:name="android.intent.action.MAIN"/>
		<category android:name="android.intent.category.LAUNCHER"/>
	</intent-filter>
	<meta-data
		android:name="android.app.shortcuts"
		android:resource="@xml/shortcuts"/>
</activity>

第二步在res目录下新建xml资源目录,并在其下新建xml文件,命名随意。这里要吐槽下,在Android Studio里新建xm文件时,居然不能选择root-element是shortcuts的,还得自己手动去敲,气人。

输入图片说明

另外还要吐槽下官方文档,上面说一个app同时可以show五个shortcuts(不管是静态还是动态),但实际上最多只能显示4个,多出的不会显示出来。

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:enabled="true"
        android:icon="@drawable/icon1_chosen"
        android:shortcutId="mall"
        android:shortcutDisabledMessage="@string/shortcutDisabledMessage"
        android:shortcutLongLabel="@string/mall"
        android:shortcutShortLabel="@string/mall">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.zhiwuya.ehome.app.ui.main.MainActivity"
            android:targetPackage="com.zhiwuya.ehome.app"/>
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.zhiwuya.ehome.app.ui.home.activity.MallServiceActivity"
            android:targetPackage="com.zhiwuya.ehome.app"/>
    </shortcut>
</shortcuts>

shortcut标签下面的属性:

android:shortcutId必须唯一;
android:icon是该shortcut的图标;
android:enabled表示该shortcus是否可用,false表示禁用,是不会在shortcuts列表中显示出来的;
android:shortcutDisabledMessage表示当shortcut拖到手机桌面固定时,而enabled又是false禁用时点击桌面上的icon时提示给用户的文字,而且此时桌面上的icon会变灰色;
android:shortcutLongLabel是在shortcuts列表中该shortcut的名字;
android:shortcutShortLabel是拖动到桌面上显示的该shortcut的名字;
intent是点击shortcut时跳转到的app内的页面,其中action是必须的,而且是能是android.intent.action.VIEW

intent可以设置多个,相当于是按顺序将这些activity放入任务栈中,最后一个activity在栈顶,因此,点击shortcut时跳转的是最后一个intent指定的activity页面,当按返回键时,则会回退到上一个intent指定的activity。

shortcut标签也可以设置多个(最多4个),在xml中从上至下的顺序,最上面的离app在桌面的icon越近,越往下越远。

动态Shortcuts

动态即在app运行时执行发布更新和移除shortcuts等操作。

private void initShortcuts() {
	if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
		return;
	}
	ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
	Intent mall = new Intent(Intent.ACTION_VIEW);
	mall.setClass(this, MallServiceActivity.class);
	mall.setPackage(BuildConfig.APPLICATION_ID);
	ShortcutI
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值