4

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.

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.

For alarm and I use AlarmManager and pending intent.

Maybe someone faced with similar problems?

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
Sergey
  • 41
  • 1
  • 3

2 Answers2

6

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

Then before run alarm check isCarUiMode result

Kenumir
  • 664
  • 8
  • 24
  • 2
    has anyone tested this? i'm always getting 'false'. – cucko May 21 '17 at 07:58
  • 1
    I've tried this, and it seems to work if you loop the check periodically in the background. Doing so allows the app to trap cases where the `com.google.android.gms.car.media.STATUS` event doesn't trigger when it logically seems like it should (in my case, I would only get `com.google.android.gms.car.media.STATUS` when manually navigating to the 'Media' section of Android Auto, which is a user-initiated action occurring _well after_ Auto actually connects). Doesn't seem ideal, but was sufficient to pass Google's review process. – aroth Jul 04 '17 at 05:14
0

As you mentioned you can register a reciever for "com.google.android.gms.car.media.STATUS" and save the status into e.g. shared preferences.

When the application wants to display the alarm, it can check the saved status. If status is connected do not show the alram, otherwise show it.

Something like:

public void onReceive(Context context, Intent intent) {
     String status = intent.getStringExtra("media_connection_status");
     boolean isConnectedToCar = "media_connected".equals(status);
     SharedPreferences sharedPrefs = context.getSharedPreferences(
            "myprefs", Context.MODE_PRIVATE);
     SharedPreferences.Editor editor = sharedPrefs.edit();
     editor.putBoolean("isConnected", isConnectedToCar);
     editor.commit();
}

And when the alarm is triggered:

SharedPreferences sharedPrefs = context.getSharedPreferences(
            "myprefs", Context.MODE_PRIVATE);
boolean isConnected = sharedPref.getBoolean("isConnected", false);
if (!isConnected) {
   // Alarm
}

Hope that helps.

hanif
  • 664
  • 5
  • 8
  • Of course I understand that. And that's what I did. If my service is running, I can easily catch it and do not show any alarms. But that is **absolutely** not solve the problem. This Broadcast Receiver is triggered **only** when started my service. This receiver **does not notify** that you connect phone to the auto android device, it comes just when your service started. But service will start only if the **user launches** the application on android-auto device. But what if phone is connected to the android auto, but user does not launch the application? Is it possible? Certainly possible. – Sergey Mar 30 '16 at 11:57
  • So the Service will not start, Broadcast will not come, and in the alarm time I wrong to think that the phone is not connected to the android auto. Thet's a problem. That's why I need a way to to know **not about the launch of my service**, I need a way to know about connecting the phone to the auto-android device. – Sergey Mar 30 '16 at 12:01
  • In your service, what do you return in your onStartCommand method? Have you tried to return Service#START_STICKY or Service#START_REDELIVER_INTENT? – hanif Mar 30 '16 at 16:12
  • For android auto it does not matter. I do not override this method. When you start the service (MediaBrowserService) onStartCommand method is not called (a few minutes ago I specifically override this method, and run the application, this method has not been called). Earlier, I wrote that android auto absolutely not obliged to run my service when the phone is connected to the auto android device and that is normal. – Sergey Mar 30 '16 at 16:38