Skip to content
Guo Yunhe edited this page May 3, 2015 · 12 revisions

Android running application

Android 5.0 (API 21) removed getRectentTask() and some other API, so we cannot use these API to get recent opend application.

However, ActivityManager.getRunningAppProcesses() can get the top running process and the package name. So we can know which application is opened. This API works with Android from API 3 to API 22 (latest).

Service

We need to create 2 service, both of them are always running:

  1. AppLogService: log application usage and save it in a file
  2. AppPredictService: predict next applications, based on trainning data read from log file. It contains 2 model, one for short period predict, the other for long period predict.

Create a service/process:

Prevent service being killed by system

Save data

We only need one log file. Every line contains:

  1. the time of opening the application
  2. the package name application
  3. the length of use time
  4. the time of leaving the application

Note: for time, we ignore the timezone. Usually the behaviours of users are only related to local time.

Example:

2015-04-01 12:22:14, org.mozilla.firefox, 720, 2015-04-01 12:34:14
2015-04-01 12:34:14, org.wikimedia.wikipedia, 720, 2015-04-01 12:46:14
2015-04-01 12:46:14, org.telegram.messenger, 720, 2015-04-01 12:58:14

Android's file storage methods:

Java file IO:

Predict algorithm

Short period and long period predict should have two predict model.

Short period prediction: decided by most recent opened applications. In a period of 30 minutes, users may switch between some specified application. We can statistics the top 90% applications, and then use KNN, or other methods, calculate the likelihood of next application.

Long period prediction: decided by scene. Time is a major factor. In the morning, noon, evening, users application choices are different. At the same time of different days, user may doing different things, such as taking on bus, working, stay home... the status of user will affect application usage results.

APPM algorithm

NOTE: The original code can only run on 32 bit Java VM, and the C++ code must be compiled to a 32 bit libary. I have tried to compile 64 bit libary and run on 64 bit Java VM, but it will cause Java VM crash.

To make the original code run on my computer (GNU/Linux, openSUSE 13.2 x64), I did following things:

Step 1: install software package.

  • g++, prelink (for compiling C++ code)
  • java-1_7_0-openjdk i386, java-1_7_0-openjdk-devel i386, java-1_7_0-openjdk-headless i386

You must install 32 bit jre to run a Java program in 32 bit mode. In openSUSE I can only choose 32 bit or 64 bit jre in offical package repo. But if you download jre RPMs from https://java.com/ , you can own both 32 bit and 64 bit jre at the same time.

Step 2: modify compile script appm-jni/compile.sh

g++ -fpic -c -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/include" -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/include/linux" -m32 Model.cpp
g++ -fpic -c -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/include" -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/include/linux" -m32 PPMd.cpp
g++ -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/include" -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0/include/linux" -shared -m32 -Wl,-soname,libappm.so Model.o PPMd.o -o libappm.so
# need install prelink package in openSUSE
execstack -c libappm.so
cp libappm.so ../libs/
rm *.o

This script is ONLY for GNU/Linux.

Step 3: run the compile script.

Step 4: modify java code PPM.java

static {
	System.load(System.getProperty("user.dir")+"/libs/libappm.so");
}

Step 5: modify Eclipse project property.

Build: openjdk-1.7 32-bit

Run: openjdk-1.7 32-bit

Step 6: run.

Test data set

Android Native Development Kits

Since APPM uses the C++ implementation of PPMd (PPMII), we need Android NDK to include it in Android application, which is written in Java.

Android NDK can compile one C++ libary into three major platforms: ARM, x86, MIPS. These .so files will be placed in different paths. When application is running, system will call the right libary for current CPU architecture of device. See

Float icon

There is a library used to create float icon like Facebook chat: