如何检测手机是否连接到android auto [英] How to detect whether the phone connected to android auto

查看:446
本文介绍了如何检测手机是否连接到android auto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个音频播放器应用程序,我需要确定用户设备何时连接到Android Auto。

I'm developing an audio player application, and I need to determine when the user's device is connected to Android Auto.

该应用程序有一个闹钟,我想要确保在用户开车时它不会消失。

The application features an alarm, and I want to make sure it doesn't go off while the user is driving.

确定我的音乐服务( MediaBrowserService )有效,我可以在onCreate和onDestroy中使用一些标志,或者注册 reciver forcom.google.android.gms.car.media.STATUS操作 - 但这是一个坏主意,因为闹钟可以在任何时间触发。而且不仅在我的音乐服务正在运行时。

To determine whether my music-service (MediaBrowserService) works, I can use some flags in onCreate and onDestroy, or register reciver for "com.google.android.gms.car.media.STATUS" action - but it's a bad idea because alarm clock can trigger in any time. And not only when my music-service is running.

对于闹钟,我使用 AlarmManager 和待定意图。

For alarm and I use AlarmManager and pending intent.

也许有人遇到过类似问题?

Maybe someone faced with similar problems?

推荐答案

你可以像google示例一样检查UIMode:

You can check UIMode like in google example:

https://github.com/googlesamples/android-UniversalMusicPlayer/blob/master/mobile/src/main/java/com/example/android/uamp/utils/CarHelper .java#L106

public static boolean isCarUiMode(Context c) {
    UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
    if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR) {
        LogHelper.d(TAG, "Running in Car mode");
        return true;
    } else {
        LogHelper.d(TAG, "Running on a non-Car mode");
        return false;
    }
}

然后在运行前报警检查 isCarUiMode 结果

Then before run alarm check isCarUiMode result

这篇关于如何检测手机是否连接到android auto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆