美文网首页
Android N的俩新特性

Android N的俩新特性

作者: VictorLiang | 来源:发表于2017-03-19 16:35 被阅读80次

自从手机换成Nexus 6P之后,系统一直是保持最新系统。因此一些新系统的特性也能体验到。
目前已经有些应用使用了Android N中的新特性。 下面分享两个比较有意思的特性。

ShortCuts

先看下效果,非常类似于iOS的3D-Touch效果,不过是长按图片出现菜单与按压力度无关。

代码也很简单:
AM.xml文件中主Activity节点添加:

<meta-data android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />

然后新建资源文件 res/xml/shortcuts.xml. 代码如下:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/new_record"
        android:shortcutDisabledMessage="@string/new_record"
        android:shortcutId="create_record"
        android:shortcutLongLabel="@string/new_record"
        android:shortcutShortLabel="@string/new_record"
        tools:targetApi="n_mr1"> <!--标记为仅支持android N-->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="点击后跳转的Activity"
            android:targetPackage="应用包名" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <!--其他快捷键-->
</shortscuts>

Tile Service

通过实现Tile Service,允许用户自己创建一个快捷操作的瓷砖图标。
效果如图:

代码也相对简单,只需集成系统提供的TileService,然后在对应的生命周期时做一些操作即可。
需要注意的是:

  • TileService可以是有状态的(可以处于开启、关闭状态),也可以是无状态的(只能点击)。可以通过对生命周期的管理来实现控制。
  • TileService 理论上可以开启应用的后台服务,但是不建议用来做应用程序的入口....
@TargetApi(Build.VERSION_CODES.N)
public class TestTileService extends TileService {
    private static final String TAG = "TestTileService";

    @Override
    public void onTileAdded() {
        super.onTileAdded();
        Log.d(TAG, "onTileAdded2: 瓷砖被添加,做一次性的初始化工作");
    }

    @Override
    public void onStartListening() {
        super.onStartListening();
        Log.d(TAG, "onStartListening2: 处于可见状态,可以更新tile,或者注册需要的监听");
    }


    @Override
    public void onClick() {
        super.onClick();
        Log.d(TAG, "onClick: On UI thread.");
        //you can start your service here
        startYourService();

        //can change icon and description
        makeAsClicked();


        //can show dialog
        Dialog dialog = new AlertDialog.Builder(getApplicationContext())
                .setMessage("test")
                .create();
        showDialog(dialog);

        //can go to specific page
        goToMainActivity();
    }

    private void makeAsClicked() {
        Tile tile = getQsTile();
        tile.setLabel("同步中");
        tile.setContentDescription("测试");
        tile.setState(Tile.STATE_ACTIVE);
        tile.updateTile();
    }

    @Override
    public void onStopListening() {
        super.onStopListening();
        Log.d(TAG, "onStopListening2: 处于不可见状态, 停止监听等");
    }

    @Override
    public void onTileRemoved() {
        super.onTileRemoved();
        Log.d(TAG, "onTileRemoved2: 瓷砖被移除,停止所有相关服务");
    }
}

<p>以上的新特性,只在Android N支持,低版本环境上不会有效果但不影响产品使用。
希望大家能善用这些新特性,不要天天搞啥黑科技,破坏游戏平衡。

相关文章

网友评论

      本文标题:Android N的俩新特性

      本文链接:https://www.haomeiwen.com/subject/mbzmnttx.html