Posts in the “android” category

A note about functional programming in Android

I spent some time last week working on an Android application, and with my newfound knowledge of functional programming (FP), I was trying to apply FP principles to my Android Activities and Fragments.

Android isn’t really meant for FP, but one thing I found that I could do is to move a lot of my business logic out of the Activities and Fragments and into separate classes, where I could often implement methods as static functions. The advantage of this is that it forces you to consciously pass variables in and out of those static functions, rather than mutating them as instance variables (think “global” variables) in your Activities and Fragments (which is a common way to handle them).

I don’t have a specific example I can share today, but when I can I’ll update this post to show specifically what I mean. In the meantime, if you try to move some of your logic out of your Activities and Fragments, I think you’ll see what I mean.

SQLite: Default a datetime field to the current time (now)

SQLite FAQ: How do I default a SQLite datetime field to the current date and time? (i.e., how do I default it to now?)

Just use the SQLite current_timestamp function, like this:

last_updated datetime default current_timestamp

In a more complete create table example I just used on an Android project, this looks like this:

How to create SQLite comments

SQLite FAQ: How do I create comments in SQLite?

SQLite lets you create comments using two different constructs, either two hyphens in sequence ("--"), or the "/.../" C-style comments. Here are examples of each approach, with the SQLite comments preceding the two database table definitions:

SQLite foreign key examples

SQLite foreign keys FAQ: Can you show me how to define foreign keys in a SQLite database table design?

The SQLite database does support foreign keys, and its foreign key syntax is similar to other databases. Here’s a quick SQLite foreign key example.

How to drop a SQLite database

SQLite database FAQ: How do I drop a SQLite database?

People used to working with other databases are used to having a DROP DATABASE command, but in SQLite there is no similar command.

The reason? In SQLite there is no "database server" — SQLite is an embedded database, and your entire database is contained in one file. So there is no need for a SQLite “drop database” command, all you have to do to drop the database is to delete the SQLite database file you were accessing.

How to create a SQLite database

SQLite database FAQ: How do I create a SQLite database?

Creating a new database in SQLite is so easy, it's amazing. Once you have SQLite installed and your PATH set up properly, from your Unix or DOS shell, just issue a SQLite command like this:

macOS Android File Transfer error: Can’t access device storage (solved)

Android/macOS Solution: This article shows a solution to the Android File Transfer app “not working on Mac” problem, where you get the Android error message, “Can’t access storage device.” (This solution may also work on Microsoft Windows systems, though I don’t have a Windows system to test with to know for sure.)

Last night I was trying to use the Android File Transfer program on my MacOS system to transfer music to my Google Nexus 9, which now runs Android 6 Android 7. After I connected my Nexus 9 tablet to the Mac with its USB cable and then started the Android File Transfer app, I saw this error message on my Mac:

Can’t access device storage. If your device’s screen is locked, disconnect its USB cable, unlock your screen, and then reconnect the USB cable.

This is what the Mac error message dialog looks like:

Android File Transfer - Can't access device storage

Unfortunately that’s a misleading and unhelpful error message, as the problem has nothing to do with the Android device screen being locked.

The solution

The solution to the problem on Android 7, Android 6 (and maybe Android 5) is to unlock your Android device (if it isn’t unlocked already), pull down the list of notifications, then tap the “USB for charging” notification:

Tapping that notification brings up the following dialog:

Transfer files (MTP)

On this dialog you want to tap the “Transfer files (MTP)” option, as indicated by that large red arrow.

Note that this option has changed names at least once. As of March, 2017, it is now labeled “Transfer files,” with the subtitle, “Transfer files to another device.”

When you do this, the Android File Transfer app will either automatically start (which it does on my Mac), or you can go ahead and start it manually, at which point you’ll see the Android File Transfer main window:

Android File Transfer app main window (success)

Once you see this window you can go ahead and start dragging files from your Mac (or Windows) computer to this Android File Transfer window.

Note: Be sure to click OK on the Mac

As Gert notes in the comments below, before you tap MTP on the phone, make sure you click “OK” on your Mac so that error message goes away.

A quick note about transferring music files

Note that if you’re transferring music files to your Android device, you’ll want to transfer them to the Music folder. To do this, double-click that folder to open it, then drag and drop music files from your PC into that folder. On a Mac you do this by opening a Finder window, navigating to the folder where your music files are located — such as /Users/Al/Music/iTunes/iTunes Music — and then dragging and dropping the files from the Mac Finder window to the Android File Transfer window. Then start the “Play Music” app on your Android device and you should see your files under the “Recent activity” area on the home page of that app.

Summary

In summary, if you get the “Can’t access device storage” error when using the Android File Transfer app when trying to transfer files from your Mac or Windows system to your Android phone or tablet, I hope this solution is helpful.

How to show an HTML string in an Android TextView

[toc]

Filed under “What I learned about Android today,” it turns out that you can display an HTML string in an Android TextView. However, this approach has major limitations, and you’ll probably want to display your HTML in a WebView instead.

Skipping past that issue for a few moments ... if you want to try to display an HTML string in a TextView, you need to use the Android Html.fromHtml() method, as shown in this code:

How to start the Android command line shell (adb)

Android FAQ: How do I start the Android command line tool (so I can interact with my Android emulator or device)?

You start the Android command line with the adb shell command:

$ adb shell

This makes at least two assumptions:

  1. You have the Android SDK installed.
  2. You have an Android emulator (or physical device) running.

When you start the adb shell, you'll see a very simple prompt that looks like this:

The Android “adb shell list files permission denied” error

As a brief note, today I tried to list the files in my Android application, which was running on a physical Android device — a Nexus 9 — with this adb shell command:

adb shell com.alvinalexander.mybrowser ls /data/data/com.alvinalexander.mybrowser

When I did that, I got an Android/ADB “permission denied” error.

The short story is that a solution to this problem is to run the same command, but with the run-as argument, like this:

adb shell run-as com.alvinalexander.mybrowser ls /data/data/com.alvinalexander.mybrowser

As a note to self, I confirmed this run-as command again in February, 2022, while working on a Flutter app:

adb shell run-as com.valleyprogramming.myapp ls /data/data/com.valleyprogramming.myapp/app_flutter

More information

The Android docs describe the run-as option:

Run commands on a device as an app (specified using the package name). This lets you run commands in adb as if the app you specify is running the command (that is, you have the same device access that the app has), without requiring root access. This might be necessary when using adb on a non-rooted device or an emulator with a Play store image. The app must be debuggable.

Related commands

I don’t have time to add much more to this right now, but one thing I’ll note is that if you have to run an ADB command where the file path has spaces in it, this command worked:

> adb shell run-as com.alvinalexander.mybrowser ls /data/data/com.alvinalexander.mybrowser/app_webview/Web\\ Data

Android emulator not loading my app

I haven't used Android in a little while now, in particular with my new laptop, and the first time I tried running an Android app from inside Eclipse, the Android emulator wouldn't finish starting properly and run my app.

I remember I used to look under the "all apps" icon, and could sometimes find my app was actually loaded, but in this case, it wasn't loaded at all.

An Android cheat sheet (my notes, main concepts)

This page is a little unusual for me; it’s basically a terse summary of what I know about Android. I created it because I tend to (a) work with Android for a few weeks or months, and then (b) get away from it for several months, so this page helps me reload everything into my brain.

[toc hidden:1]

My “Just Be” app running on a Moto 360 (Android Wear)

(Click the image for a much larger image.)

Some time in the last week or two Google announced a $99 sale on their first generation Moto 360 watches. I purchased one, and it came in the mail this morning. My main purpose for buying one is to see how my “Just Be” mindfulness reminders app (for Android) works with Android Wear smartwatches. This image shows what the notifications currently look like.

I like the way it works off the shelf, but I kinda want to get rid of the “Just Be” name on the notification. The watch shows the app icon, and that’s good enough for me. I know it probably should be there for “branding” purposes, but I’d rather just see the mindfulness reminder message. At least the app name is in a much lighter font color, that’s good.

FWIW, I don’t have an Android phone, but I do have a Google/Android Nexus 9 tablet, and everything between the tablet and watch seems to be working fine so far.

(The Moto 360 watches were available at this link on the Google Play store, but the last time I checked they were out of stock.)