您的位置:首页 > 移动开发 > Android开发

【学以致用】android功能实现5---android8.0 Launcher获取快捷方式源码分析(1)

2017-12-26 18:46 1996 查看
从其他应用往桌面创建快捷方式,android8.0统一采用requestPinShortcut的方式。 对于桌面而言,是怎么从requestPinShortcut获取快捷方式信息在桌面创建快捷方式呢?

 

Android8.0的快捷方式参数不再通过广播传送,而是存放在系统当中。创建快捷方式的应用将信息放入系统,而系统则生成shortcut ID等信息,传入桌面应用。

 

桌面创建快捷方式由两部分完成。

1:获取快捷方式的名字和图标,然后将两者作为快捷方式放到桌面上,提供移动拖拽点击等服务。桌面应用接收ID等数个信息,在桌面创建快捷方式,其中的图片等资源需要桌面通过ID等信息去系统里面获取。

2:放置在桌面上的快捷方式有intent这个参数存入数据库,而intent参数一个核心功能是启动该应用。配置合理的intent并使其能够启动。点击启动应用的时候,也是通过ID等信息,去系统中启动。

 

实际操作流程如下:

首先需要创建一个activity,这个activity会在其他应用调用requestPinShortcut的时候,会在框架中通过一个service启动一个activity。

该activity包含android.content.pm.action.CONFIRM_PIN_SHORTCUT和android.content.pm.action.CONFIRM_PIN_APPWIDGET

在Launcher的manifest里面这样写。

 

<activity android:name="com.android.launcher3.dragndrop.AddItemActivity"

    android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.Alert"

    android:excludeFromRecents="true"

    android:autoRemoveFromRecents="true"

    android:label="@string/action_add_to_workspace" >

    <intent-filter>

        <action android:name="android.content.pm.action.CONFIRM_PIN_SHORTCUT" />

        <action android:name="android.content.pm.action.CONFIRM_PIN_APPWIDGET" />

    </intent-filter>

</activity>

 

这样当其他应用点击在桌面创建快捷方式时,就会弹出这个activity,Launcher源码将本activity的设为style/Theme.DeviceDefault.Light.Dialog.Alert, 这样弹出的activity就是一个对话框。

 

接着在activity中设立一个按钮,点击的话,会自动创建快捷方式,这个按钮主要是从intent获取mRequest,在使用 InstallShortcutReceiver.queueShortcut来处理mRequest.getShortcutInfo()中的shortcut。

public void onPlaceAutomaticallyClick(View v) {

    if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT) {

        InstallShortcutReceiver.queueShortcut(

                new ShortcutInfoCompat(mRequest.getShortcutInfo()), this);

        logCommand(Action.Command.CONFIRM);

        mRequest.accept();

        finish();

        return;

    }

 

上面涉及两个重点方法:

一个是:

InstallShortcutReceiver.queueShortcut( new ShortcutInfoCompat(mRequest.getShortcutInfo()), this);

 用来将外界传入的shortcut参数放入快捷方式创建列队中,用来创建跨界方式。

 

另一个是:

mRequest.accept(); 

该方法会传递给系统,表示桌面接收了本次信息,并将为本次信息创建快捷方式。有了这个信息,后面才可以通过ID信息去系统里面查找,图片等参数。

 

将intent中获取的shortcutinfo由ShortcutInfoCompat处理一下,其实ShortcutInfoCompat和shortcutinfo是一样的,ShortcutInfoCompat是用来将shortcutinfo的信息更好的被Launcher使用。

 

public ShortcutInfoCompat(ShortcutInfo shortcutInfo) {

    mShortcutInfo = shortcutInfo;

}

 

随后创建PendingInstallShortcutInfo。里面涉及到shortcutInfo
、user、launchIntent、 label
的赋值

 

public PendingInstallShortcutInfo(ShortcutInfoCompat info, Context context) {

    activityInfo = null;

    shortcutInfo = info;

    providerInfo = null;

    data = null;

    mContext = context;

    user = info.getUserHandle();

    launchIntent = info.makeIntent();

    label = info.getShortLabel().toString();

}

其中shortcutInfo
、user和 label是直接获取,而launchIntent
是调用了makeIntent方法:

该方法,从shortcutInfo获取了的activity、package和id这三个参数,剩下为默认配置参数。

 

return new Intent(Intent.ACTION_MAIN)

        .addCategory(INTENT_CATEGORY)

        .setComponent(getActivity())

        .setPackage(getPackage())

        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)

        .putExtra(EXTRA_SHORTCUT_ID, getId());

 

本intent是索引快捷方式的关键,用来在手机中定位究竟是什么快捷方式。

 

总结,创建快捷方式主要由第三方应用调用requestPinShortcut来启动包含android.content.pm.action.CONFIRM_PIN_SHORTCUT和android.content.pm.action.CONFIRM_PIN_APPWIDGET的附属于Launcher的activity。

启动该activity的时候,就会传入一个intent。Intent包含第三方应用的包名,分辨快捷方式的ID。

如果Launcher接收了该快捷方式,则通过intent获取PinItemRequestCompat,调用其accept()方法,表示用户确定要创建该快捷方式,随后系统将该快捷方式存入手机中。

而Launcher本身也获取快捷方式的信息,主要是packgakename和ID。用来创建具体的快捷方式图标
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐