您的位置:首页 > 移动开发 > Android开发

Android的关机调试步骤记录

2011-12-28 23:25 253 查看
您所在的位置 :
IT客 » 程序开发 »
Android

Android的关机调试步骤记录

http://www.itkee.com/developer/detail-d3.html

1.通过logcat找到关机流程

frameworks/base/core/java/com/android/internal/app/ShutdownThread.java

public void run() {

boolean bluetoothOff;

boolean radioOff;

BroadcastReceiver br = new BroadcastReceiver() {

@Override public void onReceive(Context context, Intent intent) {

// We don't allow apps to cancel this, so ignore the result.

broadcastDone();

}

};

Log.i(TAG, "Sending shutdown broadcast...");

// First send the high-level shut down broadcast.

mBroadcastDone = false;

mContext.sendOrderedBroadcast(new Intent(Intent.ACTION_SHUTDOWN), null,

br, mHandler, 0, null, null);

final long endTime = System.currentTimeMillis() + MAX_BROADCAST_TIME;

synchronized (mBroadcastDoneSync) {

while (!mBroadcastDone) {

long delay = endTime - System.currentTimeMillis();

if (delay <= 0) {

Log.w(TAG, "Shutdown broadcast timed out");

break;

}

try {

mBroadcastDoneSync.wait(delay);

} catch (InterruptedException e) {

}

}

}

Log.i(TAG, "Shutting down activity manager...");

final IActivityManager am =

ActivityManagerNative.asInterface(ServiceManager.checkService("activity"));

if (am != null) {

try {

am.shutdown(MAX_BROADCAST_TIME);

} catch (RemoteException e) {

}

}

final ITelephony phone =

ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));

final IBluetooth bluetooth =

IBluetooth.Stub.asInterface(ServiceManager.checkService(

BluetoothAdapter.BLUETOOTH_SERVICE));

final IMountService mount =

IMountService.Stub.asInterface(

ServiceManager.checkService("mount"));

try {

bluetoothOff = bluetooth == null ||

bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF;

if (!bluetoothOff) {

Log.w(TAG, "Disabling Bluetooth...");

bluetooth.disable(false); // disable but don't persist new state

}

} catch (RemoteException ex) {

Log.e(TAG, "RemoteException during bluetooth shutdown", ex);

bluetoothOff = true;

}

try {

radioOff = phone == null || !phone.isRadioOn();

if (!radioOff) {

Log.w(TAG, "Turning off radio...");

phone.setRadio(false);

}

} catch (RemoteException ex) {

Log.e(TAG, "RemoteException during radio shutdown", ex);

radioOff = true;

}

Log.i(TAG, "Waiting for Bluetooth and Radio...");

// Wait a max of 32 seconds for clean shutdown

for (int i = 0; i < MAX_NUM_PHONE_STATE_READS; i++) {

if (!bluetoothOff) {

try {

bluetoothOff =

bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF;

} catch (RemoteException ex) {

Log.e(TAG, "RemoteException during bluetooth shutdown", ex);

bluetoothOff = true;

}

}

if (!radioOff) {

try {

radioOff = !phone.isRadioOn();

} catch (RemoteException ex) {

Log.e(TAG, "RemoteException during radio shutdown", ex);

radioOff = true;

}

}

if (radioOff && bluetoothOff) {

Log.i(TAG, "Radio and Bluetooth shutdown complete.");

break;

}

SystemClock.sleep(PHONE_STATE_POLL_SLEEP_MSEC);

}

// Shutdown MountService to ensure media is in a safe state

try {

if (mount != null) {

mount.shutdown();

} else {

Log.w(TAG, "MountService unavailable for shutdown");

}

} catch (Exception e) {

Log.e(TAG, "Exception during MountService shutdown", e);

}

//shutdown power

Log.i(TAG, "Performing low-level shutdown...");

Power.shutdown();

}

logcat显示到最后一步了,但是系统还没有关闭,是电源关闭有问题。

2. Power.shutdown();

找这个 android.os.Power 模块

frameworks/base/core/java/android/os/power.java 中有调用shutdown()jni接口

3. jni接口

frameworks/base/core/jni/android_os_Power.cpp

static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)

{

sync();

#ifdef HAVE_ANDROID_OS

reboot(RB_POWER_OFF);

#endif

}

其中,RB_POWER_OFF 及RB_AUTOBOOT (重启)定义bionic/libc/include/sys/reboot.h

#define RB_AUTOBOOT LINUX_REBOOT_CMD_RESTART

#define RB_HALT_SYSTEM LINUX_REBOOT_CMD_HALT

#define RB_POWER_OFF LINUX_REBOOT_CMD_POWER_OFF

系统最后会执行系统调用sys_reboot(reboot)。reboot通常定义在内核kernel/kernel/sys.c

4 。内核部分。 kernel/sys.c

其中,

if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)

cmd = LINUX_REBOOT_CMD_HALT;

lock_kernel();

switch (cmd) {

case LINUX_REBOOT_CMD_RESTART:

kernel_restart(NULL);

break;

case LINUX_REBOOT_CMD_CAD_ON:

C_A_D = 1;

break;

case LINUX_REBOOT_CMD_CAD_OFF:

C_A_D = 0;

break;

case LINUX_REBOOT_CMD_HALT:

kernel_halt();

unlock_kernel();

do_exit(0);

break;

case LINUX_REBOOT_CMD_POWER_OFF:

printk("test powe down in %s(%d)\n", __FILE__, __LINE__);

kernel_power_off();

unlock_kernel();

do_exit(0);

break;

当pm_power_off为空时, 由上层传入的命令LINUX_REBOOT_CMD_POWER_OFF会变为LINUX_REBOOT_CMD_HALT,从而执行kernel_halt()函数。在该函数中,会执行函数machine_halt()。错误原因就在这儿(pm_power_off为空)。

函数指针pm_power_off是与平台相关的指针,

添加函数

static void lpc32xx_power_off(void)

{

__raw_writel((0x1<<9),GPIO_P2_OUTP_CLR(GPIO_IOBASE));

}

在ea3250_board_init中添加

pm_power_off = lpc32xx_power_off;

编译内核 ,OK

android层可以正常关闭电源了。。,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: