Mohit Lakhanpal
Mohit Lakhanpal

Reputation: 1505

Android auto: The Task can't be completed while driving

I have started working on an Android Auto feature for an app, where I want to add an alert for the driver when something arrives. For this, I am using the Message Template, which contains a title, a logo, and a body message. Below is the implementation of the app

 public class CarScreen extends Screen {

private static String message = "We will notify you!!";

private int icon = R.drawable.ic_ts_icon_logo;

private int templateType = 1;


public CarScreen(CarContext carContext) {
    super(carContext);

    // Initialize the BroadcastReceiver
    CarBroadcastReceiver carBroadcastReceiver = new CarBroadcastReceiver(this);

    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter();
    filter.addAction(Constants.ANDROID_AUTO_ALERT);
    filter.addAction(Constants.ANDROID_AUTO_ARROW);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        carContext.registerReceiver(carBroadcastReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
    } else {
        carContext.registerReceiver(carBroadcastReceiver, filter);
    }

}


@NonNull
@Override
public Template onGetTemplate() {
    Template template = null;
    if (templateType == 1) {

        CarIcon carIcon = CommonFunctions.createCarIcon(getCarContext(), icon);

        template = new MessageTemplate.Builder(message)
                .setTitle("Warning")
                .setHeaderAction(Action.APP_ICON)
                .setIcon(carIcon)
                .build();

    } else if (templateType == 2) {


        iconList.clear();
        counterList.clear();
    }

    assert template != null;
    return template;

}


public void updateUI(String newMessage, int newIcon) {
    // Update the UI based on the received data
    message = newMessage;
    this.icon = newIcon;
    templateType = 1;
    invalidate();
}}

In Android Manifest:

  <service
        android:name=".auto.CarService"
        android:label="@string/app_name"
        android:icon="@drawable/ic_ts_icon_logo"
        android:permission="androidx.car.app.ACCESS_SURFACE"
        android:exported="true">
        <intent-filter>
            <action android:name="androidx.car.app.CarAppService" />
           <category android:name="androidx.car.app.category.IOT" />

        </intent-filter>

    </service>

 <meta-data
        android:name="com.google.android.gms.car.application"
        android:resource="@xml/automotive_app_desc" />


    <meta-data
        android:name="androidx.car.app.minCarApiLevel"
        android:value="1" />

    <meta-data
        android:name="androidx.car.app.carApiLevel"
        android:value="1" />

automotive_app_desc file:

 <?xml version="1.0" encoding="utf-8"?><automotiveApp>
<uses name="template" />

I have tested the same feature on the desktop head unit, and it was working fine. However, I am encountering an issue where the task cannot be completed while driving, especially during the app download from the Play Store.

I am not sure how to replicate the issue at the debug level on my local setup. If anyone has a solution or any ideas, your input would be greatly appreciated.

Upvotes: 0

Views: 78

Answers (0)

Related Questions