0

我目前正在尝试通过 Android Auto 显示我的 Ev 百分比。我跑不动了CarInfo carInfo = getCarContext().getCarService(CarHardwareManager.class).getCarInfo();。我在我的汽车 build.gradle 中使用了它useLibrary 'android.car'并尝试导入很多东西,但这显然没有帮助。这是我的第一个文件:

package com.example.aatest4;

import android.car.Car;
import android.car.CarInfoManager;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.car.app.CarAppMetadataHolderService;
import androidx.car.app.CarAppService;
import androidx.car.app.CarContext;
import androidx.car.app.Screen;
import androidx.car.app.Session;
import androidx.car.app.hardware.CarHardwareManager;
import androidx.car.app.hardware.info.CarInfo;
import androidx.car.app.validation.HostValidator;
//import android.content.ServiceConnection;




public class HelloWorldService extends CarAppService {
    @NonNull
    @Override
    public HostValidator createHostValidator() {
        return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR;
    }

    public Session onCreateSession() {
        return new Session() {
            @NonNull
            @Override
            public Screen onCreateScreen(@NonNull Intent intent) {
            
              CarInfo carInfo = getCarContext().getCarService(CarHardwareManager.class).getCarInfo();

                return new HelloWorldScreen(getCarContext());
            }
        };
    }
}

这是我的第二个:

package com.example.aatest4;

//import android.car.Car;
//import android.car.CarInfoManager;

import androidx.annotation.NonNull;
import androidx.car.app.CarContext;
import androidx.car.app.Screen;
import androidx.car.app.model.Pane;
import androidx.car.app.model.PaneTemplate;
import androidx.car.app.model.Row;
import androidx.car.app.model.Template;

public class HelloWorldScreen extends Screen {
    //public HelloWorldScreen() {
    public HelloWorldScreen(CarContext carContext) {
        super(carContext);
    }
    @NonNull
    @Override
    public Template onGetTemplate() {
        String akku = String.valueOf(50);
        Row row = new Row.Builder().setTitle(akku + "%   ").addText(akku + "%").build();
        return new PaneTemplate.Builder(new Pane.Builder().addRow(row).build()) .setTitle("Akkuanzeige").build();
        //Todo: Center?
    }
}
4

0 回答 0