目录

(Optional) Developing the Log Recording Service

Overview

You can specify a fixed log directory where log files are stored for analysis and troubleshooting.

Assigning App Permissions

To use APIs of the log recording service, you need to declare the READ_EXTERNAL_STORAGE (reading data from external storage) and WRITE_EXTERNAL_STORAGE (writing data to external storage) permissions in the AndroidManifest.xml file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The preceding permissions are dangerous permissions. You need to dynamically apply for these permissions (according to dangerous permission requirements in Android 6.0 or later).

Enabling Log Recording

You can pass the log file path and related parameter settings using the LogConfig object, and then call setLogConfig() to enable log recording.

  1. Call the getSettingsClient() method to obtain a SettingsClient instance.
    Java
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    Kotlin
    var settingsClient = LocationServices.getSettingsClient(this)
  2. Set log recording parameters.
    Java
    String filePath = getApplicationContext().getFilesDir().getAbsolutePath() + "/log";
    LogConfig logConfig = new LogConfig();
    // Set the path for storing log files.
    logConfig.setLogPath(filePath);
    Kotlin
    val filePath = applicationContext.filesDir.absolutePath + "/log"
    val logConfig = LogConfig()
    // Set the path for storing log files.
    logConfig.setLogPath(filePath)
  3. Call setLogConfig() to enable log recording.
    Java
    settingsClient.setLogConfig(logConfig).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            // TODO: If no error information is returned, log recording is enabled successfully.
        }
    });
    Kotlin
    settingsClient!!.setLogConfig(logConfig).addOnFailureListener {
        // TODO: If no error information is returned, log recording is enabled successfully.
    }

To change the path for storing log files after enabling log recording, stop the app process for which log recording has been enabled, pass a new path for storing log files, and enable log recording. When you stop the app process, the log recording function will be disabled and logs will no longer be recorded.