基于AOSP 8.0.1源码分析force-stop的实现细节。

使用 force-stop

am force-stop com.example

com.example 是应用程序包名。

am命令

cat /system/bin/am
#!/system/bin/sh
if [ "$1" != "instrument" ] ; then
    cmd activity "$@"
else
    base=/system
    export CLASSPATH=$base/framework/am.jar
    exec app_process $base/bin com.android.commands.am.Am "$@"
fi

am 是一个shell脚本。如果第一 …

背景

在阅读Android Framework代码时,有时需要动态调试加深理解,或者想调试某个API的实现原理(如startActivity)时,希望可以使用Android Studio调试,且可以对应到源码的每一行,故需要搭建一个调试环境。

可选方 …

实现 pow(x, n) ,即计算 \(x\) 的整数 \(n\) 次幂函数(即,\(x^n\) )。

示例 1:

输入:x = 2.00000, n = 10

输出:1024.00000

示例 2:

输入:x = 2.10000, n = 3

输出:9.26100

示例 3:

输入:x = 2.00000, n = -2

输出:0.25000

解释 …

C++ 继承

export NDK_ROOT=~/android-ndk-r18b

${NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
  --target=x86_64-none-linux-android
  --gcc-toolchain=${NDK_ROOT}/toolchains/x86_64-4.9/prebuilt/linux-x86_64
  --sysroot=${NDK_ROOT}/sysroot
  -isystem ${NDK_ROOT}/sysroot/usr/include/x86_64-linux-android
  -pie -o  hello.c.o -c hello.c

${NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
  --target=x86_64-none-linux-android
  --gcc-toolchain=${NDK_ROOT}/toolchains/x86_64-4.9/prebuilt/linux-x86_64
  --sysroot  ${NDK_ROOT}/platforms/android-21/arch-x86_64
  -pie hello.c.o -o hello

  ${NDK_ROOT …

Android Unicode 字符串

相关源码

String16

// http://androidxref.com/6.0.1_r10/xref/system/core/libutils/String16.cpp#allocFromUTF8
static char16_t* allocFromUTF8(const char* u8str, size_t u8len)
{
    const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
    utf8_to_utf16(u8cur, u8len, u16str);
}

namespace android{

//! This is a string holding UTF-16 characters.
class String16{
private …

Android 智能指针

相关源码

http://androidxref.com/6.0.1_r10/xref/system/core/include/utils/StrongPointer.h http://androidxref.com/6.0.1_r10/xref/system/core/include/utils/RefBase.h http://androidxref.com/6.0.1_r10/xref/system/core/libutils/RefBase.cpp

StrongPointer

INITIAL_STRONG_VALUE 为什么不是0

区分从来没有指针引用该对象,还 …

Android 源码开发

准备开发环境

使用docker-aosp搭建

编译步骤

source build/envsetup.sh 
including device/asus/deb/vendorsetup.sh
including device/asus/flo/vendorsetup.sh
including device/asus/fugu/vendorsetup.sh
including device/generic/mini-emulator-arm64/vendorsetup.sh
including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh
including device/generic/mini-emulator-mips/vendorsetup.sh
including device/generic/mini-emulator-x86/vendorsetup.sh
including device/generic/mini-emulator-x86_64/vendorsetup …

Docker 基础

Dockerizing a Python Flask App

Install nginx 1.27.2

docker pull nginx:1.27.2
docker pull python:3.13
services:
  web:
    container_name: example_nginx
    image: nginx:1.27.2
    privileged: true
    ports:
      - 80:80
      - 443:443
    volumes:
      - "$PWD/html:/usr/share/nginx/html:rw"
      - "$PWD/conf/conf.d:/etc/nginx/conf.d"
      - "$PWD/logs:/var/log/nginx"
      - "$PWD/certs:/etc/nginx/certs"

自 …

Android ClassLoader

Class

// http://androidxref.com/6.0.1_r10/xref/libcore/libart/src/main/java/java/lang/Class.java
public final class Class<T> implements Serializable, AnnotatedElement, GenericDeclaration, Type {
    static native Class<?> classForName(String className, boolean shouldInitialize,
        ClassLoader classLoader) throws ClassNotFoundException;
}

VMClassLoader

// http://androidxref.com/6.0.1_r10/xref/libcore/libart/src/main/java/java/lang/VMClassLoader.java
package java.lang;
class VMClassLoader{
    native static Class …

Android ProgressBar Circle

Shape

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] 
    android:innerRadius="integer"
    android:innerRadiusRatio="integer"
    android:thickness="integer"
    android:thicknessRatio="integer"
    android:useLevel="boolean"
    >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float …