android7.1 shotcuts,Android N App Shotcuts 学习

()一:

Android 7.1 增加了很多新特性,比如多窗口,App Shotcuts….

其中,App Shoutcuts就类似于iphone手机上3D Touch功能,在应用桌面长按某个app图标,就会对应的弹出选项,使我们操作更加方便和快速;

先来张图看看安卓的App Shotcuts长什么样:

0818b9ca8b590ca3270a3433284dd417.png

图片引用官网,长按短信图标弹出的选项。

二:

自己实践为自己的app添加App Shortcuts功能,前提条件必须是android 7.1,api 25

App ShortCuts 实现有两种:

1,xml 布局

2,代码实现

先来讲xml布局实现:

1.首先在自己的清单文件(activity 主入口)下,加入一个 标签

2.然后再res目录下新建一个xml文件夹,随后创建一个xml文件,取名就是上面的引用

android:shortcutId="add_website" //id

android:icon="@drawable/ic_launcher" //图标

android:shortcutShortLabel="@string/shortcuts"

android:shortcutLongLabel="@string/shortcutss" //名字

>

android:action="com.example.android.appshortcuts.ADD_WEBSITE"

android:targetPackage="com.example.administrator.myapplication" //包名

android:targetClass="com.example.administrator.myapplication.MainActivity" //类名

/>

3.到这一步,基本上就已经完成了App Shortcuts的功能,然后打开7.1模拟器run

0818b9ca8b590ca3270a3433284dd417.png

4.这样就可以了,如果想添加多个选项,怎么办?依然在xml文件中修改(注意,shortcutiId不要一样)

android:shortcutId="add_website"

android:icon="@drawable/ic_launcher"

android:shortcutShortLabel="@string/shortcuts"

android:shortcutLongLabel="@string/shortcutss"

>

android:action="com.example.android.appshortcuts.ADD_WEBSITE"

android:targetPackage="com.example.administrator.myapplication"

android:targetClass="com.example.administrator.myapplication.MainActivity"

/>

android:shortcutId="add_website1"

android:icon="@drawable/ic_launcher"

android:shortcutShortLabel="@string/shortcuts"

android:shortcutLongLabel="@string/shortcutss"

>

android:action="com.example.android.appshortcuts.ADD_WEBSITE"

android:targetPackage="com.example.administrator.myapplication"

android:targetClass="com.example.administrator.myapplication.MainActivity"

/>

android:shortcutId="add_website2"

android:icon="@drawable/ic_launcher"

android:shortcutShortLabel="@string/shortcuts"

android:shortcutLongLabel="@string/shortcutss"

>

android:action="com.example.android.appshortcuts.ADD_WEBSITE"

android:targetPackage="com.example.administrator.myapplication"

android:targetClass="com.example.administrator.myapplication.MainActivity"

/>

如下图:

0818b9ca8b590ca3270a3433284dd417.png

三:

如果代码实现?其实也很简单,如下

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")

.setShortLabel("Web site")

.setLongLabel("Open the web site")

.setIcon(Icon.createWithResource(context, R.drawable.icon_website))

.setIntent(new Intent(Intent.ACTION_VIEW,

Uri.parse("https://www.mysite.example.com/")))

.build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值