问题
I'm an Android developer and I'm trying to develop a custom Android Auto app, that does a simple mirroring of the phone screen. I know that currently the API are only available for music and messaging apps, but I would write an app for mirror a simple "hello world". I follow the Google Getting Started tutorial and I'm using the Desktop Head Unit (DHU) provided by Google (at developer.android.com/training/auto/testing/index.html)
but when I tap last button on the bottom of the display and select "All car apps", my application doesn't appear on the list.
All car apps
For example, if Android Auto is launched in a Samsung tablet (SM-T555), the DHU lists these app:
com.google.android.gms, Maps, System UI, Video, SampleAuthenticatorService, SecureSampleAuthService, Screen capture, Android Auto, Phone, Media, Return to Google, Samsung Billing, Google App, Google Play Music, Music
Available Car Apps in a Samsung Tablet
How can I make an app that is displayed on the available app list in Android Auto? Is possible do mirroring for a custom app in Android Auto?
回答1:
Create a service like this:
public class HelloWorldService extends CarActivityService {
@Override
public Class<? extends CarActivity> getCarActivity() {
return HelloWorldAutoActivity.class;
}
}
Then add the service to your manifest like this:
<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>
Finally create a xml file called automotive_app_desc under your res folder:
<automotiveApp>
<uses name="service" />
<uses name="projection" />
<uses name="notification" />
</automotiveApp>
Your HelloWorldAutoActivity.class will work as your MainActivity.
回答2:
In order to have app displayed on the Auto. You will have to upload it to playstore on beta channel.
回答3:
The Main Activity File
The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application. Following is the default code generated by the application wizard for Hello World! application −
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);
}
}
The Manifest File
Whatever component you develop as a part of your application, you must declare all its components in a manifest.xml which resides at the root of the application project directory. This file works as an interface between Android OS and your application, so if you do not declare your component in this file, then it will not be considered by the OS. For example, a default manifest file will look like as following file −
<?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>
The Strings File
The strings.xml file is located in the res/values folder and it contains all the text that your application uses. For example, the names of buttons, labels, default text, and similar types of strings go into this file. This file is responsible for their textual content. For example, a default strings file will look like as following file −
<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>
The Layout File
The activity_main.xml is a layout file available in res/layout directory, that is referenced by your application when building its interface. You will modify this file very frequently to change the layout of your application. For your "Hello World!" application, this file will have following content related to default layout −
<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>
来源:https://stackoverflow.com/questions/42625017/develop-an-android-auto-custom-app