Tag: mobile development

  • Spoofing Battery and Forcing Sleep (Android Development)

    I wanted to do something with my Android phone using ADB commands, while keeping it plugged in to run the said commands. However, the thing I wanted to test was related to the idle (dozing or asleep) mode of Android devices. The workaround was to spoof the battery state to bypass Android’s charging restrictions.

    Spoofing Battery Charging Status and Forcing Android Device to Sleep (Idle Mode)

    • Turn off the device screen using the physical power button.
    • Trick Android into thinking the device is running on battery power: adb shell dumpsys battery unplug
    • Force the device to step into deep Doze mode: adb shell dumpsys deviceidle force-idle
    • Verify the device is successfully spoofed and asleep: adb shell dumpsys deviceidle get deep (This should return IDLE).

    Can also do:
    adb shell dumpsys battery unplug && adb shell dumpsys deviceidle force-idle && adb shell dumpsys deviceidle get deep

    Device Cleanup for Spoofed Battery and Forced Sleep (Crucial!)

    1. Once testing is done, you must tell the OS to start reading the actual battery state again, otherwise it won’t charge properly: adb shell dumpsys battery reset
    2. Bring the device out of idle: adb shell dumpsys deviceidle unforce

    Can also do:
    adb shell dumpsys battery reset && adb shell dumpsys deviceidle unforce && adb shell dumpsys deviceidle get deep

  • Add “options menu” to an AppCompatActivity (Android App Development)


    Adding an “options menu” to an Android app’s Activity is quite simple.

    This is an example of what an Android options menu looks like. Look at the icon that looks like 3 dots vertically stacked.

    android options menu

    My dev environment status as of writing this

    Android Studio version: v. 2.1
    OS: Mac OS X El Capitan v. 10.11
    My Android app build status:

    • compileSdkVersion: 24
    • buildToolsVersion: 24.0.2
    • minSdkVersion: 16
    • targetSdkVersion: 24

    Add “options menu” to an Android AppCompatActivity

    #1 Create menu layout file

    Create res/menu/main.xml, creating the menu folder if not yet existing.

    Add the following to res/menu/main.xml:

    #2 Enable menu by adding MenuInflater to the Activity

    Add the following overriding method to your AppCompatActivity:

    #3 Add listener for selected options menu item

    Again, add the following overriding method to your AppCompatActivity. Replace the Toasts with whatever you need to do upon menu item selection:

    That’s it! Your Android Activity should now have an options menu on the actionbar

    My resources for adding my options menu to Activity

    Adding the App Bar
    Android SDK: Implement an Options Menu

  • Android app development: click a ListView item programmatically


    I’m going to share a code snippet that you may use to programmatically click a ListView item in your Android app. This will not simply highlight the desired ListView item, but also trigger the onItemClick listener.

    This code is what I placed in a Fragment’s onLoadFinished() method:


    Simply replace the default_position value with the index number of the ListView item you want to click/select programmatically.

    That item should be highlighted (assuming you have like, touch selectors) and also trigger the onItemClick listener on Android.


    A possible error is . If this happens, wrap your code around a Runnable(), like this:

  • Android app dev: Add ActionBar to Preference Activity

    When I converted an activity into PreferenceActivity, I noticed that its ActionBar disappeared. I’ll show you how I brought the ActionBar back to my PreferenceActivity.

    In this guide I will assume that the name of the file that contains your is SettingsActivity.java, and that the view assigned to your SettingsActivity.java is named .

    First, create settings_toolbar.xml under /app/src/main/res/layout/ and place the following code into it:

    Then add the following overridden method to SettingsActivity.java:

    The Java method programmatically creates a Toolbar object from the XML toolbar at settings_toolbar.xml.

    Reference(s): Creating a Preference Screen with support (v21) Toolbar

  • Android app dev: ShareActionProvider in Activity & Fragment

    I’ve been taking the Developing Android Apps course on Udacity, and yesterday I encountered a difficulty in solving the Quiz: Share Intent.

    One of the course’s goal is to build the Sunshine App – an Android app with weather forecast.

    In that quiz I had to implement ShareActionProvider on my DetailsFragment so that a share icon becomes available on my app’s ActionBar.

    One of the main difficulties was the error that stated ActionProvider cannot be cast into ShareActionProvider. And at some point there was also an error saying that the methods/classes I’m using require API 14, while my minimum supported API is 9. The Udacity course required that I set it to 9 so I couldn’t change that.

    Now, the sharing in my app is working file, and I will share how I did it via Gist. I actually ended up taking a peek at the code provided by Developing Android Apps course on Udacity because I didn’t want to waste any more time doing trial-and-error (I have another project to work on! lol), but I still had to edit their code for ShareActionProvider here and there to make it bug-free.

    And so, take a look at how my Sunshine app codes ended up:

  • Android M runtime permissions utility class


    Hello world, I prepared a PermissionsUtil.java class and it’s on GitHub.

    Check it out: AndroidPermissionsUtil

    Why use it? It will help by letting you write less code as you add conditions to your Android app that supports Android 6.0 (Marshmallow). The utility class defines helper methods that were used in the following example.

    Sample usage of Android M PermissionUtils

    On the activity where you need to check for runtime permissions, add these:

    And add the callback function to the same activity:

    Feel free to use in on any project. And please let me know if you have suggestions on how to improve that Android M runtime permissions utility class.

  • Android app codes for Google Maps with clustering

    I prepared Android app codes with Google Maps and clustering feature. Feel free to use these as basis for your new projects.

    They are on GitHub:

    Android map clustering with default markers

    [1] Default Map Markers:
    catzie/SimpleAndroidMapClustering

     

     

    I simply took the necessary classes and res files from Android Maps Utility demo, and then placed them into bare Android Studio projects for simplicity. For me, it’s easier to start a new “maps with clustering” project on simple apps like these, rather than downloading the entire Android Maps Utility Demo, picking out the necessary files, and fixing errors before getting started each time. 😆

     

  • Android Maps: Get current location coordinates with Google API Client Builder

    To get your current location via Google Map in an Android app, this was how we do it:

    Stop. Don’t use that! The method GoogleMap.getMyLocation() is deprecated. Instead, we need to use FusedLocationApi and the callback onLocationChanged().

    Here’s an example code that works for me ( consider copying the contents of onResume() into onStart() ):

    Code source: How to get current location of device on google maps api v2, getMyLocation() method is deprecated

  • Android Studio: add Google Map from empty activity (dev note)

    //Update: I prepared a simple Android app that features a Google Map with markers that form clusters together when zoomed out. Check it out: Simple Android Map Clustering on GitHub.

    This “dev note” of mine will give you a rough idea on how to add a Google Map to your Android up from an empty activity, not from a pre-made Google Maps activity.

    I won’t be able to write everything in detail here because I’m in a hurry, and so I’m calling this article a “dev note” instead of tutorial. 🙂 This dev note can be considered a “guide” too.

    This dev note was based on Android Studio v.2.1 running on MAC OS X

    From your Android Studio’s SDK Manager, install Google Play Services.

    Add this as child of <application> tag in AndroidManifest.xml:

    Later we’ll replace YOUR_API_KEY with a key from Google API Console.

    Also add these to your AndroidManifest.xml as direct child of manifest tag:

    We need to get an API KEY for the value attribute of this meta data. Let’s get one from the Google API Console — create a new project there and then search for Google Maps Android API. Enable it.

    After enabling it, you’ll find a way to get an API key that we need from Google API Console. Go to Credentials -> Create Credentials -> API Key -> Android Key. Before we are given the API key, we need to provide Google API Console a SHA1 certificate fingerprint from our computer and our app’s package name.

    To generate SHA1 certificate fingerprint on a Mac, type this on the terminal:

    On Windows, try this command:

    When prompted for a keystore password, enter the word debug as your password.

    Copy the SHA1 certificate fingerprint and then paste it into Google API Console. Enter your package name as well (e.g. com.yourdomain.projname). You’ll be given an API Key in return.

    Note: My Android Studio says ‘In the Google Developer Console (https://console.developers.google.com), ensure that the “Google Maps Android API v2” is enabled.’ but I can’t find the option for V2.

    Remember the meta-data tag we added in the AndroidManifest.xml? Replace the value with the API key that you just copied from Google API Console.

    Resource: https://developers.google.com/maps/documentation/android-api/signup

    Open app/build.gradle file and add the following as dependency:

    Sync your project with Gradle files.

    This was one of the tutorials I followed: GOOGLE MAPS ANDROID API V2 TUTORIAL USING ANDROID STUDIO and I based my XML plus Java codes on the author’s.

    My files in the end…

    Here were the codes of my Android app when I finally made the Google Map show up in the activity:

    app/build.gradle

    build.gradle

    app/src/main/res/layout/activity_main.xml

    app/src/main/java/net/catzie/maptest/MainActivity.java

    app/src/main/AndroidManifest.xml

    …even though the Google Map now shows up on my Android activity, I get these errors that I’m not able to fix yet:

  • Get device’s IMEI in Android app development

    Here’s the content of my MainActivity class where I log the IMEI of my test device.

    You also need to add this permission to your AndroidManifest.xml file:

    Place it inside the <manifest></manifest> tag, but same level as <application></application>.

    Source: How to get the device’s IMEI/ESN programmatically in android?