android

android tips and tutorials

My Android AsyncTask docs and examples (parameters, callbacks, executing, canceling) alvin July 20, 2017 - 3:23pm

I’ve currently written this document as a “note to self” about how the Android AsyncTask works. It’s currently incomplete, but if you want to know how an AsyncTask works, most of the answers are generally here. I provide documentation for most aspects of the AsyncTask, though my coverage of (a) updating progress/status and (b) canceling an AsyncTask is a little weak atm.

Source code for an Android AsyncTask (REST client) example alvin July 20, 2017 - 12:41pm

I just got back into using an Android AsyncTask, and it took me a little while to re-load the concepts in my head. I used AsyncTask’s a few years ago, but haven’t used them since.

To help remember how they work, I created a little AsyncTask example project, and I’ve included all of the source code for that project here. I’ll show all of the source code for my classes and configuration files, and then explain the code at the end.

Android Room database persistence library examples alvin July 20, 2017 - 12:02pm

As a note to self, here are some Android Room database persistence library examples:

Those tutorials don’t show how to properly use Room database access methods, so they’ll lead to Android “Application Not Responding” (ANR) errors. Therefore, here are some related Google/Android docs:

Finally, here’s my own Android AsyncTask REST example, which also shows how to use an AsyncTask.

An example of Android StrictMode output (with improper database access) alvin July 20, 2017 - 11:49am

I was just working with an example of how to use Android’s new Room Persistence Library, and the example I was working with ran some of its code on the main Android thread, also known as its “UI thread.” I knew this was bad, but I wanted to start with someone’s example, and then figure out a good way to get the Room method calls to run on a background thread, such as using an AsyncTask. (The Android docs don’t specify a “best practice” for this atm.)

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:

The Remix OS from Jide is dead alvin July 17, 2017 - 4:41pm

On the same day I learned that the Cookie Lady of Denver died, I just read that the Remix OS from Jide is also dead. (In inner circles I’m known as a bit of a cookie monster, so I’m sad that I never met or even knew of the Cookie Lady.)

Android: How to reference a drawable image in XML (or Java) alvin July 15, 2017 - 3:51pm

When you need to reference a drawable image from an Android XML file, such as a layout or menu file, use this tag:

android:src="@drawable/myimage"

That assumes that you have a file named myimage.png in your res/drawable directories. As a more complete example, this shows how I reference an image named images_show.png in an Android menu item:

How to enable a physical Android device (phone or tablet) for development and debugging alvin June 14, 2017 - 7:28pm

To enable a physical Android hardware device (phone or tablet) for developing and debugging your apps with Android Studio, follow these steps.

1) Enable “Developer Options”

The first thing you have to do with a new Android device is to enable the developer options in the Settings app. To do this:

How to turn off the Android haptic feedback vibration setting alvin March 12, 2017 - 5:30pm

If you want to turn off the Android haptic feedback vibration setting on Android 7, I just found that you have to follow these steps:

Settings > Sound > Other sounds > Vibrate on tap

It feels odd that the setting is buried under the “Sound” setting, but there it is.