【问题标题】:Develop an Android Auto custom app开发 Android Auto 自定义应用
【发布时间】:2017-03-06 11:55:28
【问题描述】:

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

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

All car apps

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

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

Available Car Apps in a Samsung Tablet

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

【问题讨论】:

    标签: android-auto


    【解决方案1】:

    创建这样的服务:

    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。

    【讨论】:

    • 您能否提供信息:CarActivityService 来自哪里?我在 Android 中没有看到任何具有该名称的服务。您还没有提到您需要扩展 CarActivity 的 Activity?有这门课来的吗?
    • 您需要从Android Auto中提取并制作一个dex2jar,然后将其导入您的项目并将其设置为文件依赖
    • @EmilBorconi 您在将转换后的 dex 链接到您的项目时遇到问题吗?.....我收到一个错误,gradle 说它无法将 zip 文件提取到 tmp 目录.....
    • @J1and1 - 尝试使用这个 jar:app.box.com/s/v13xfv32qtkdafo4splwltime6ut4l22 并像这样导入它:import com.google.android.apps.auto.sdk.*;我没有机会研究可用的类/函数/等,因为它是逆向生成的,不要指望任何官方支持
    • @J1and1 不客气,如果你设法找到一些事情,我很想听听。我还没有时间开始深入研究它,但希望在不久的将来会自己做。
    【解决方案2】:

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

    【讨论】:

    • 不正确。您必须激活development mode,然后在Android Auto 本身上打开unknown services
    • @Przemo 我做了这个,应用程序没有显示在 android auto 上。
    • @Dansp 老实说,我不知道从那以后汽车发生了什么变化,所以帮不了你:/
    • @Przemo 是的,它现在不工作,很伤心:'(
    【解决方案3】:

    主活动文件

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

    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 操作系统和您的应用程序之间的接口,因此如果您未在此文件中声明您的组件,则操作系统不会考虑它。例如,默认清单文件将如下所示 -

    <?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 文件夹中,它包含您的应用程序使用的所有文本。例如,按钮、标签、默认文本和类似类型的字符串的名称将进入此文件。该文件对其文本内容负责。例如,默认字符串文件将如下所示 -

    <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!”应用程序,该文件将包含以下与默认布局相关的内容 -

    <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>
    

    【讨论】:

    • 这个答案完全错过了,甚至没有接近解决这个问题。如果问题是“我如何为 Android 创建一个 hello world 应用程序?”,这个答案会很好。但是这个问题是关于 Android Auto 的,它是一个特定的 API,用于集成应用程序以与 Android Auto 兼容的主机一起使用。这个答案应该被删除。
    • 这根本没有回答他的问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多