8

我是一名 Android 开发人员,我正在尝试开发一个自定义 Android Auto 应用程序,它可以简单地镜像手机屏幕。我知道目前 API 仅适用于音乐和消息应用程序,但我会编写一个应用程序来镜像一个简单的“hello world”。我遵循 Google 入门教程,我使用的是 Google 提供的 Desktop Head Unit (DHU)(位于 developer.android.com/training/auto/testing/index.html)

但是当我点击显示屏底部的最后一个按钮并选择“所有汽车应用程序”时,我的应用程序没有出现在列表中。

所有汽车应用

例如,如果 Android Auto 在三星平板电脑 (SM-T555) 中启动,则 DHU 会列出以下应用:

com.google.android.gms、地图、系统 UI、视频、SampleAuthenticatorService、SecureSampleAuthService、屏幕截图、Android Auto、电话、媒体、返回 Google、Samsung Billing、Google 应用、Google Play 音乐、音乐

三星平板电脑中可用的汽车应用程序

如何制作显示在 Android Auto 可用应用列表中的应用?是否可以在 Android Auto 中为自定义应用程序进行镜像?

4

3 回答 3

13

创建这样的服务:

public class HelloWorldService extends CarActivityService {

    @Override
    public Class<? extends CarActivity> getCarActivity() {
        return HelloWorldAutoActivity.class;
    }
}

然后像这样将服务添加到您的清单中:

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

    <service
        android:name=".HelloWorldService"
        android:exported="true"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="com.google.android.gms.car.category.CATEGORY_PROJECTION" />
            <category android:name="com.google.android.gms.car.category.CATEGORY_PROJECTION_OEM" />
        </intent-filter>
    </service>

最后在你的 res 文件夹下创建一个名为 Automotive_app_desc 的 xml 文件:

<automotiveApp>
    <uses name="service" />
    <uses name="projection" />
    <uses name="notification" />
</automotiveApp>

您的 HelloWorldAutoActivity.class 将作为您的 MainActivity。

于 2017-03-15T14:56:14.823 回答
-1

为了让应用程序显示在汽车上。您必须将其上传到 beta 频道的 Playstore。

于 2018-02-16T05:12:23.450 回答
-9

主要活动文件

主要活动代码是一个 Java 文件 MainActivity.java。这是最终转换为 Dalvik 可执行文件并运行您的应用程序的实际应用程序文件。以下是应用程序向导为 Hello World 生成的默认代码!应用程序 -</p>

package com.example.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

清单文件

无论您开发作为应用程序一部分的任何组件,都必须在位于应用程序项目目录根目录的 manifest.xml 中声明其所有组件。该文件用作 Android 操作系统和您的应用程序之间的接口,因此如果您未在此文件中声明您的组件,则操作系统不会考虑它。例如,默认清单文件将如下所示 -</p>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tutorialspoint7.myapplication">

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">

      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

字符串文件

strings.xml 文件位于 res/values 文件夹中,它包含应用程序使用的所有文本。例如,按钮、标签、默认文本和类似类型的字符串的名称将进入此文件。该文件对其文本内容负责。例如,默认字符串文件将如下所示 -</p>

<resources>
   <string name="app_name">HelloWorld</string>
   <string name="hello_world">Hello world!</string>
   <string name="menu_settings">Settings</string>
   <string name="title_activity_main">MainActivity</string>
</resources>

布局文件

activity_main.xml 是 res/layout 目录中可用的布局文件,您的应用程序在构建其界面时会引用该文件。您将非常频繁地修改此文件以更改应用程序的布局。为您的“Hello World!” 应用程序,该文件将包含以下与默认布局相关的内容 -</p>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:padding="@dimen/padding_medium"
      android:text="@string/hello_world"
      tools:context=".MainActivity" />

</RelativeLayout>
于 2017-06-27T10:05:20.977 回答