2
关于Android Auto如何在用户将设备插入汽车后获得回拨?我想根据用户实际连接到android auto触发一个动作发生,有可能吗?Android Auto - 用户连接移动设备时回拨
关于Android Auto如何在用户将设备插入汽车后获得回拨?我想根据用户实际连接到android auto触发一个动作发生,有可能吗?Android Auto - 用户连接移动设备时回拨
您应该能够获得USB连接广播。从那里你将不得不编写自己的逻辑来找出Android Auto在前台。 (可能需要引入一些延迟,以防安卓系统自动运行需要一段时间,启动程序似乎是谷歌播放服务的一部分)
这是我在我的服务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);