2015-11-06 42 views

回答

2

您应该能够获得USB连接广播。从那里你将不得不编写自己的逻辑来找出Android Auto在前台。 (可能需要引入一些延迟,以防安卓系统自动运行需要一段时间,启动程序似乎是谷歌播放服务的一部分)

2

这是我在我的服务onCreate方法中做的(右侧示例代码):

IntentFilter filter = new IntentFilter(CarHelper.ACTION_MEDIA_STATUS); 
mCarConnectionReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String connectionEvent = intent.getStringExtra(CarHelper.MEDIA_CONNECTION_STATUS); 
     mIsConnectedToCar = "media_connected".equals(connectionEvent); 
     LogHelper.i(TAG, "Connection event to Android Auto: ", connectionEvent, 
       " isConnectedToCar=", mIsConnectedToCar); 
     if(mIsConnectedToCar) { 
      if(mService == null) { 
       bindMusicService(); 
       if (mService != null) { 
        try { 
         initMediaMetaData(); 
         toggleMediaPlaybackState(mService.isPlaying()); 
        } catch (RemoteException e) { 
         Log.d(TAG, e.toString()); 
        } 
       } 
      } 
     } 
    } 
}; 
registerReceiver(mCarConnectionReceiver, filter); 
相关问题