Android Auto - 用户连接移动设备时回拨

时间:2015-11-06 17:05:21

标签: android-auto

关于Android auto,当用户将设备插入汽车时,如何获得回调?我想根据用户实际连接到android auto的时间触发一个动作,是否可能?

2 个答案:

答案 0 :(得分:2)

您应该能够进行USB连接广播。从那里你必须编写自己的逻辑来确定Android Auto是在前台。 (可能需要引入稍微延迟,以防android自动需要时间来启动。启动器似乎是谷歌播放服务的一部分)

答案 1 :(得分:2)

以下是我在我的服务onCreate方法中做到的方法(右边是示例代码):

IntentFilter filter = new IntentFilter(CarHelper.ACTION_MEDIA_STATUS);
mCarConnectionReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String connectionEvent = intent.getStringExtra(CarHelper.MEDIA_CONNECTION_STATUS);
        mIsConnectedToCar = "media_connected".equals(connectionEvent);
        LogHelper.i(TAG, "Connection event to Android Auto: ", connectionEvent,
                " isConnectedToCar=", mIsConnectedToCar);
        if(mIsConnectedToCar ) {
            if(mService == null) {
                bindMusicService();
                if (mService != null) {
                    try {
                        initMediaMetaData();
                        toggleMediaPlaybackState(mService.isPlaying());
                    } catch (RemoteException e) {
                        Log.d(TAG, e.toString());
                    }
                }
            }
        }
    }
};
registerReceiver(mCarConnectionReceiver, filter);