2

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

4

2 回答 2

2

您应该能够获得 USB 连接广播。从那里您将不得不编写自己的逻辑来确定 Android Auto 是否在前台。(可能需要稍微延迟,以防 android auto 需要时间来启动。启动器似乎是 google play 服务的一部分)

于 2015-12-14T15:19:24.933 回答
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);
于 2015-12-30T23:37:56.250 回答