Posts in the “android” category

A Java method to log Android memory use

As a quick note today, here’s a little Java method that I use to log Android memory use (RAM use) from an Activity or Fragment:

private void logMemoryInfo(Context context, String TAG) {
   ActivityManager activityManager = (ActivityManager) context.getSystemService(getActivity().ACTIVITY_SERVICE);
   int memoryClass = activityManager.getMemoryClass();
   ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
   activityManager.getMemoryInfo(memoryInfo);

   Log.i(TAG, "\n------------ RAM -------------");
   Log.i(TAG, "mem class: " + memoryClass);
   Log.i(TAG, "mem avail: " + memoryInfo.availMem);
   Log.i(TAG, "low mem:   " + memoryInfo.lowMemory);
   Log.i(TAG, "threshold: " + memoryInfo.threshold);

   long mb = 1024*1024;
   Runtime runtime = Runtime.getRuntime();
   Log.i(TAG, "Used Memory:  " + (runtime.totalMemory() - runtime.freeMemory()) / mb);
   Log.i(TAG, "Free Memory:  " + runtime.freeMemory()  / mb);
   Log.i(TAG, "Total Memory: " + runtime.totalMemory() / mb);
   Log.i(TAG, "Max Memory:   " + runtime.maxMemory()   / mb);
}

Android: How to go back to Google App stories after closing the Now card

I like the “Google” app on Android — the thing you see if you swipe right on the Android home screen. But a weakness of it is that you can’t get back to a story easily. For instance, this morning I followed a Google Now card to see a story about Tom Ricketts and the Cubs, closed the story, then thought, “Wait, I meant to look at XYZ in that web page.” Once you close a story like this the Now card disappears, and you can’t get back to it easily (which is the weakness).

Solution 1: Going back to Google Now app stories on Android 7

I don’t know if this is the only way to do it, but as a solution, one way to get back to the story on Android 7 is to follow these steps:

How to make a phone call from your Android app

I came across this Android phone dialer tip yesterday. If you want to make a phone call from an Android application, all you have to do is create a new Intent, either an Intent.ACTION_DIAL (to start the call) or Intent.ACTION_CALL (to place the call).

Here are the three lines of source code you need to get started:

Intent dialIntent = new Intent();
dialIntent.setAction(Intent.ACTION_DIAL);
dialIntent.setData(Uri.parse("tel:8675309"));

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:

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]

How to transfer very large files to an Android device with adb

Today I learned that the Android File Transfer tool has a 4GB data transfer limit, but I was able to get around that by using the adb (Android Debug Bridge) command. The short story is that I used this adb command to transfer a 6GB file to my Android system, an old Nexus 9:

adb push ABigMovie.iso /sdcard/Movies

The bad news is that VNC choked and died when trying to play the movie, but the good news is that I found a good way to transfer very large files to an Android device. (I wasn’t really trying to go too far down the road trying to upload DVD images to my Nexus 9, I just wanted to see if I could upload very large files to an Android device. (You can’t fit many ISO images on an old Android device.))

In a related note, I found the Android /sdcard/Movies path through a little trial and error. For future reference, I found that this command lists all of the directories on an Android device:

adb ls /sdcard

The output of that command looks like this:

$ adb ls /sdcard

000041f9 00001000 5a31ec5e .
000041c9 00001000 58c58c32 ..
000041f9 00001000 58c58c57 Android
000041f9 00001000 58dc6424 Music
000041f9 00001000 58c58c3e Podcasts
000041f9 00001000 58c58c3e Ringtones
000041f9 00001000 58c58c3e Alarms
000041f9 00001000 58c5d2d4 Notifications
000041f9 00001000 58c5d902 Pictures
000041f9 00001000 5a31ecc6 Movies
000041f9 00001000 5a01e0fe Download
000041f9 00001000 5998ecef DCIM
000081b0 00000010 58dc6301 Music:.pos
000041f9 00001000 59b28d95 Temp
000041f9 00001000 59866d57 kindle
000041f9 00001000 592cf85d Books
000041f9 00001000 59b28d15 .RecycleBin

You can also learn a little more about the Android filesystem reality with a few more commands. After running adb shell, I poked around the filesystem a little, like this:

flounder:/ $ ls -al /sdcard
lrwxrwxrwx 1 root root 21 1969-12-31 17:00 /sdcard -> /storage/self/primary

flounder:/ $ ls -a /storage/self/primary
.  .RecycleBin Android DCIM     Movies Music:.pos    Pictures Ringtones kindle 
.. Alarms      Books   Download Music  Notifications Podcasts Temp    

Finally, here are two good ADB resources I found whilst poking around the interwebs:

How to enable a physical Android device (phone or tablet) for development and debugging

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:

  • Open Settings
  • Scroll down to “About phone” or “About tablet”
  • Tap that item (“About phone” or “About tablet”)
  • Tap the “Build number” field seven times

In Android 7, as you tap “Build number,” you’ll see it count down, which is a nice touch. When you finish with this you’ll see a message that says something like, “You’re now a developer.”

2) Developer options > USB debugging

Next:

  • Go back to the main Settings menu, where you’ll see a new “Developer options” menu item. Tap that.
  • Scroll down to the “USB debugging” setting, and enable it. This needs to be enabled so you can upload your Android apps to your physical hardware device.

Your Android device should be ready to use

At this point your device should show up in the “Select Deployment Target” window when you click “Run app” in Android Studio. If it doesn’t show up, there is one more step you need to take on your device.

The short version of this goes like this:

  • Pull down the Android settings window on your physical device.
  • There should be an “Android System” notification there. Tap it.
  • Select the “Transfer files” option on that notification setting.

(I describe this solution more in my Android File Transfer error: Can’t access device storage (solved) tutorial.)

Summary: How to enable a physical Android device for development and debugging

At the time of this writing this android.com article, Configure On-Device Developer Options, wasn’t 100% clear about this process, so I wrote this article to save me some time in the future. I hope it helps you as well.

How to create an Android Color from a hexadecimal/HTML string (#fff)

Android FAQ: How can I create a Color from a hexadecimal color string in Android?

The Android Color.parseColor method

Solution: Use the Android Color.parseColor method, like this:

int color = Color.parseColor("#519c3f");

I just used it like this in my code, where I directly set the Paint color:

paint.setColor(Color.parseColor("#519c3f"));

The string shown is a hexadecimal/HTML color string which represents a light shade of green. The important part of this is that I liked that color in a prototype, then got the HTML/hexadecimal color string using Gimp, and then could use that string directly in my Android/Java application to create a color, without having to manually convert the color string to RGB.

Using an alpha value with Color.parseColor

To add an alpha value, just add two more hex values at the front of the string. This alpha setting (FF) means the color is “fully on,” i.e., it is not transparent at all:

paint.setColor(Color.parseColor("#ff519c3f"));

This alpha setting (22) means the color is almost fully transparent:

paint.setColor(Color.parseColor("#22519c3f"));

Standard Android colors (Color class docs)

For the record, the full package for the Android Color class is android.graphics.Color. Also, those docs state:

Supported formats are: #RRGGBB #AARRGGBB or 
one of the following names:
 
'aqua'
'black'
'blue'
'cyan'
'darkgray'
'darkgrey'
'fuchsia'
'gray'
'green'
'grey'
'lightgray'
'lightgrey'
'lime'
'magenta' 
'maroon'
'navy'
'olive'
'purple'
'red'
'silver'
'teal'
'white'
'yellow'

Creating Android colors using RGB values

Also, while I’m in the neighborhood, this is one way to create an Android Color instance using RGB values:

g.setColor(Color.rgb(130, 130, 130));

Google is introducing a Neural Networks API in Android 8.1 developer preview

Several media outlets are reporting that Google is introducing their Neural Networks API in developer preview of Android 8.1. TechCrunch has a well-written article that includes this:

“The big highlight here is the new Neural Networks API, which brings hardware-accelerated inference to the phone for quickly executing previously trained machine learning models. Bringing these calculations to the edge can bring a lot of utility to the end user by reducing latency and load on the network, while also keeping more sensitive data on-device.”

Making the switch, from iPhone to Android

The comical part of my recent surgery was that my iPhone 5S kept dying, both before and after the surgery. Something is going on where the iPhone completely loses reception, and the only way to fix it — the only hope of fixing it — is to completely restore the phone using a secret handshake technique they taught me at the local Apple Store. (They also told me this technique only works about 10% of the time, and kindly suggested I buy a new iPhone because the 5S is almost four years old.)

Before the surgery the phone was completely dead for 2+ days, so I was driving around like Jim Rockford, borrowing other people’s phones and getting in touch with people in person. I had to talk to my surgeon, two groups at the hospital, a caregiver service, etc.

Skipping past that ... I decided to give an Android phone a chance, so I bought a relatively cheap Moto E phone, pictured here next to the dying iPhone 5S. I’ll get the Moto activated tomorrow, and I hope to give it at least a month. I’ve used Android tablets for years, but this is my first Android phone.

How to draw a rectangle in Android (using onDraw method of View)

Android FAQ: How do I draw a rectangle in Android?

To draw a rectangle in Android you’ll need to create your own View, i.e., a class that extends the Android View class. For example, this CustomView shows how to extend a View and then use the Rect and Paint classes along with the onDraw method to draw a rectangle: