Does there exist an intent filter which indicates when Android Auto starts? am building an app that start background thread, and I want connect to a device using Bluetooth to get remote control of head unit from custom hardware.
You can use ACTION_ENTER_CAR_MODE in a broadcast receiver to listen for when Android Auto starts and connects. Just keep in mind that ACTION_ENTER_CAR_MODE is not exclusive to Android Auto, it just means the OS is in car mode, which may or may not involve Android Auto.
Also, to satisfy the Android O requirements, you’ll need to make an explicit registration of the receiver by registering it in the activity. As a result of registering it in the activity it will not receive the broadcast on the very first connection to Auto, but only after the activity has been created and then on each connection afterwards.
<receiver
android:name=".CarModeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.app.action.ENTER_CAR_MODE"/>
<action android:name="android.app.action.EXIT_CAR_MODE"/>
</intent-filter>
</receiver>
Then in the implementation of the receiver...
public class CarModeReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
Log.d("CarModeReceiver", "Entered Car Mode");
} else if (UiModeManager.ACTION_EXIT_CAR_MODE.equals(action)) {
Log.d("CarModeReceiver", "Exited Car Mode");
}
}
}
It's also worth noting that from the documentation linked above...
In addition, the user may manually switch the system to car mode without physically being in a dock. While in car mode -- whether by manual action from the user or being physically placed in a dock -- a notification is displayed allowing the user to exit dock mode. Thus the dock mode represented here may be different than the current state of the underlying dock event broadcast.