Addenda

Changes that happened after publication of the book.

.

Ch 4. Persistence

External Storage

Accessing external storage (sd-card) even when only reading, has become very complicated starting with Android 6, and even more so with Android 10. Hence the Ch4 Android Studio Project has been created to be compatible with version 19 of Android.  For this when creating a new project, you should insist on backward compatbility on the first screen, then after the project has been created basically delete any references to any libraries in the settings, etc.

A shortcut is to request permission directly:

ActivityCompat.requestPermissions(
    FileServerActivity.this, 
    new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
    23
);

After the user grants the permission, sometimes the app works directly, sometimes it has to be restarted.  There is also a way to avoid the restart, but that is a little more tricky.

[] The Storage Situation: External Storage, https://commonsware.com/blog/2019/10/08/storage-situation-external-storage.html
[] Accessing getExternalStorageDirectory, https://stackoverflow.com/questions/34865206/accessing-getexternalstoragedirectory

Database Navigator

To see SQLite databases created on your device, you can use the Database Navigator plugin for Android Studio [1].

To install: from File>Settings select Plugins and search for Database Navigator, and install it.  After a restart you should see a new "DB Navigator"  menu point.  Now use the Device File Explorer to download the friends.db file to your computer.  In the DB Browser, open a new connection by clicking on the '+', select SQLite.  Here under Database files, click on the 'sqlite.db' and replace it by the location of friends.db file.  Click on 'Test Connection' to make sure it works.  Now should see the Schemas etc.  If you want to execute SQL commands, open a connection under Consoles.

[1] Browse SQLite database in Android Studio https://medium.com/@mattyskala/browse-sqlite-database-in-android-studio-4fbba6cca105

.

Ch 5. Sensors

Location

In addition to the permissions in the Android manifest, you now also need to request permission explicitely in the code:

String[] perms = {
    Manifest.permission.ACCESS_FINE_LOCATION, 
    Manifest.permission.ACCESS_COARSE_LOCATION
};
ActivityCompat.requestPermissions(this, perms, 42);

.

Ch 7. Networking

Multicast

For multicast to work you need to acquire a multicast lock.  For this you need to get the following permission in the Android manifest,

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

and add the following code to acquire the lock:

try {
    WifiManager wifi = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifi != null) {
        WifiManager.MulticastLock lock = wifi.createMulticastLock("YoActivity");
        lock.acquire();
    }
} catch (Exception e) {
    Log.d("YoActivity", "" + e.getMessage().toString());
}

and finally, don't forget to tell the socket to go into broadcast mode:

theSocket.setBroadcast(true);

.

ST 2. Library

First, we show how to create an Android Library project (aar):

  • create a normal Android project
  • follow steps "Convert an app module to a library module" in [1]:
    • open the module-level build.gradle file
    • delete the line for the applicationId
    • at the top of the build.gradle file, you should see the following:
            apply plugin: 'com.android.application'
      change it to the following:
            apply plugin: 'com.android.library'
  • this should create the file '/app/build/outputs/aar/app-debug.aar'

[1] https://developer.android.com/studio/projects/android-library

Second, we show how to use an Android Library in our own projects. 

  • create a normal project
  • copy the 'app-debug.aar' file into the /app/libs/ directory
  • in the build.gradle file, modify the fileTree line to read like this:
      dependencies {
        implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
        ...
      }

.