2

In the Google Play Console there is a very helpful page “Android vitals” / “ANRs & crashes”. The information provided under “crashes” is almost helpful (e.g. NullPointerException in "filename, line number"). That is something to work with. However, the info under “ANRs” (Activity Not Responding) is mostly mysterious.

For example, I have written an app which needs read/write access to the external storage – which is not necessarily a removable SD Card. Since Android 4.2 (API 17), access to the external SD Card is not possible any more. Instead, there is a link to “/mnt/sdcard” or "/storage/emulated/0", which resides in the internal memory. (Why this is called “external storage” is Google’s secret.)

Of course I have set the required entries in AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

and of course, since Android 6 (Marshmallow), I am handling the permissions correctly with

requestPermissions(requPerms, myRequestCode);

and

onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)

To determine the storage path for the app data files (for example "/storage/emulated/0"), I use the following code:

public static File getMountName()
{      
    File mount = null;

    // possible ANR 1
    String state = Environment.getExternalStorageState();

    if (state.equals(Environment.MEDIA_MOUNTED))
    {
         // possible ANR 2
         File path = Environment.getExternalStorageDirectory();
         mount = new File(path.getAbsolutePath());
    }

    return mount;
}  // getMountName

For some reason, some of the user’s smartphones are hanging with “ANR” in one of the two lines “Environment.getExternalStorage…” marked with the respective comment. (Which means, if they hang on the 2nd line, they have passed the 1st one successfully.)

The related error messages in Android vitals are shown in the following examples:

for ANR 1:

Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 2. Wait queue head age: 14805.0ms.)

and for ANR 2:

Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{33def99 u0 net.braun_home.sensorrecording.lite/net.braun_home.sensorrecording.SensorService}

Does anybody have an idea, what these messages mean, and why they appear on some smartphones only? According to Android vitals, they all have newer Android versions 8 or 9. I myself have also two devices with these versions and the app runs perfectly there.

  • You never have to call getExternalSorageState() nowadays so you can remove that. – blackapps Mar 05 '20 at 13:44
  • `File path = Environment.getExternalStorageDirectory(); mount = new File(path.getAbsolutePath())` You can simplify that to `mount = Environment.getExternalStorageDirectory();`. – blackapps Mar 05 '20 at 13:45
  • Please keep in mind, that my app is supposed to run on old versions of Android as well. I started with 4.0.3 (Ice Cream Sandwich), arrived now at 9.0 (Pie) and my ambition is, to support all the versions. Furthermore, even if I shrink the code to this single line, this probably does not avoid the ANR. – Michael Braun Mar 05 '20 at 14:28

0 Answers0