Showing posts with label Android Debug Bridge (adb). Show all posts
Showing posts with label Android Debug Bridge (adb). Show all posts

Monday, October 15, 2018

Monitor memory related events with onTrimMemory(), and simulate memory low in Android Emulator using adb

Android can reclaim memory from your app or kill your app entirely if necessary to free up memory for critical tasks. To help balance the system memory and avoid the system's need to kill your app process, you can implement the ComponentCallbacks2 interface and override onTrimMemory() callback method to listen for memory related events when your app is in either the foreground or the background, and then release objects in response to app lifecycle or system events that indicate the system needs to reclaim memory. 

When the system begins killing processes in the LRU cache, it primarily works bottom-up. The system also considers which processes consume more memory and thus provide the system more memory gain if killed. The less memory you consume while in the LRU list overall, the better your chances are to remain in the list and be able to quickly resume.

Here is a example code to implement the ComponentCallbacks2 interface and override onTrimMemory() callback method.

package com.blogspot.android_er.androidontrimmemory;

import android.content.ComponentCallbacks2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

/*
http://android-er.blogspot.com/
Example to use onTrimMemory()
 */
public class MainActivity extends AppCompatActivity
        implements ComponentCallbacks2 {

    private static final String TAG = "MyActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.v(TAG, "onCreate() called");
    }

    public void onTrimMemory(int level){
        Log.v(TAG, "onTrimMemory(" + level + ")");
        switch(level){

            case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
                Log.v(TAG, "TRIM_MEMORY_UI_HIDDEN");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
                Log.v(TAG, "TRIM_MEMORY_RUNNING_MODERATE");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
                Log.v(TAG, "TRIM_MEMORY_RUNNING_LOW");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
                Log.v(TAG, "TRIM_MEMORY_RUNNING_CRITICA");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
                Log.v(TAG, "TRIM_MEMORY_BACKGROUND");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
                Log.v(TAG, "TRIM_MEMORY_MODERATE");
                break;
            case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
                Log.v(TAG, "TRIM_MEMORY_COMPLETE");
                break;

            default:
                Log.v(TAG, "default");
        }
    }

}


To simulate the memory low condition, you can use the adb command:

adb shell am send-trim-memory

Example for my case, it's:

adb shell am send-trim-memory com.blogspot.android_er.androidontrimmemory MODERATE

Notice that you cannot set background level if your app is in foreground. Otherwise the following exception will be thrown, as shown in the video.

java.lang.IllegalArgumentException: Unable to set a background trim level on a foreground process



reference:
Overview of memory management
Manage your app's memory





Wednesday, March 2, 2016

Monkey - Generates pseudo-random user events to test your Android App


The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.

read details in Android document - UI/Application Exerciser Monkey

Example to run "adb shell monkey ...", to test my example.


Thursday, January 7, 2016

Display ABI using "adb shell getprop ro.product.cpu.abi" command


Different Android handsets use different CPUs, which in turn support different instruction sets. Each combination of CPU and instruction sets has its own Application Binary Interface, or ABI. The ABI defines, with great precision, how an application's machine code is supposed to interact with the system at runtime.

~ reference: http://developer.android.com/ndk/guides/abis.html


To identify the android device is 32-bit or 64-bit 

Android is going to support 64-bit, but there are so many android devices, how do developer know current device is 32-bit  or 64-bit  ?

Let's begin from  this command:

adb shell getprop ro.product.cpu.abi

getprop is an android utility to retrieve a property via the android property service.

~ referene: https://software.intel.com/en-us/blogs/2014/12/16/how-to-identify-the-image-is-32-bit-or-64-bit-user-space


This video show how the command "adb shell getprop ro.product.cpu.abi" run on device and emulator.




Related:
List supported ABIs programmatically

Friday, January 1, 2016

List installed app and location using adb shell


To list all installed app and their installed location, enter the adb command:
adb shell pm list packages -f


Monday, September 7, 2015

Install Xiaomi RedMi 2 (紅米手機2) USB and ADB driver, on Windows

To install RedMi 2 (紅米手機2) USB and ADB drivers, tested on Windows 10:
- Plug in your phone to PC, using USB cable. You will find a CD Driver, click to run the Setup.


- It will install the Xiaomi Assistance program, USB and ADB driver.



To enable USB debug, you have to allow USB debugging on your phone.


To enable ADB Wireless debugging, your PC and your phone should be in the same WiFi network:
- Switch to platform-tools directory, normally in "C:\Program Files (x86)\Android\android-sdk\platform-tools"
- Run "adb devices", you should find the usb connected device.
- Run "adb tcpip 5555" to reset the tcpip connection.
- Run "adb connect <your phone's IP>" to connect adb with wireless
- Run "adb devices" again, you should find both the usb connected and WiFi connected devices.
- Now, you can remove the USB connection, and debug with ADB wirelessly.



Tuesday, September 1, 2015

ADB (Android Debug Bridge) connect Android device via WiFi in Windows 10

You can connect ADB with Android devices via WiFi to develop apps.



You can find the adb tool in <sdk>/platform-tools/
in my case, it is in
C:\Users\user\AppData\Local\Android\sdk\platform-tools

To connect to Android devices, enter the command:
>adb connect 192.168.1.107
where 192.168.1.107 is the IP address of Android devices.

Most probably, cannot connect. Connect Android devices to PC with USB cable, wait it to be connected.

Enter the command to reset tcpip:
>adb tcpip 5555

Then connect again:
>adb connect 192.168.1.107

After connected, you can disconnect USB cable.

Sunday, March 29, 2015

adb command to reboot Android device

To deboot connect devies using adb, enter the commnad:
$ adb shell am broadcast -a android.intent.action.BOOT_COMPLETED


Sunday, September 7, 2014

Android ADB on WiFi: unable to connect to

For any reason, adb (via Wifi) cannot connect to device with error:

unable to connect to <ip address>:<port>

Try to restarts the adbd (on device) daemon listening on TCP on the specified port, by running adb command tcpip, with USB connected to device.

$ ./adb tcpip 5555

then connect again.

$ ./adb connect 192.168.1.111


Related:
Setup Android adb for Wifi debug

Friday, September 5, 2014

Android ADB device offline

When I run the adb command to list devices, it show my connected device with "offline", and I cannot issue any command on it.

My solution is to run Task Manager (on Ubuntu) and kill the running adb, then re-run adb again, problem solved:)


Thanks for Vladislav Ivanov comment. You can also enter the command:

adb kill-server

and then
adb start-server



Related:
- Setup Android adb for Wifi debug

Friday, August 29, 2014

Setup Android adb for Wifi debug



To enable Android Debug Bridge (adb) debug via WiFi, connect IP address with the command:

$ adb connect <Android device IP>

or

$ adb connect <Android device IP>:<port>

example:
$ adb connect 192.168.1.111
(the default port number is 5555)

remark:
In my own case, i need to run the command adb tcpip (with usb connected) before connect at the first time:
adb tcpip 5555


FAQ:
- Android ADB device offline
- unable to connect to <ip address>:<port>