当前位置:操作系统 > 安卓/Android >>

Android关机流程解析

在PowerManager的API文档中,给出了一个关机/重启接口:


public void reboot (String reason)
对于这个接口的描述很简单,就是几句话。

接口的作用就是重启设备,而且,就算重启成功了也没有返回值。

需要包含REBOOT权限,也就是android.permission.REBOOT

唯一参数reason代表需要的特定重启模式,比如recovery,当然也可以为null。


--------------------------------上层空间--------------------------------

1.frameworks/base/core/java/android/os/PowerManager.java

[java] 
 * Reboot the device.  Will not return if the reboot is
 * successful.  Requires the {@link android.Manifest.permission#REBOOT}
 * permission.
 *
 * @param reason code to pass to the kernel (e.g., "recovery") to
 *               request special boot modes, or null.
 */ 
public void reboot(String reason) 
{    
    try { 
        mService.reboot(reason); 
    } catch (RemoteException e) { 
    }    
}  

    /**
     * Reboot the device.  Will not return if the reboot is
     * successful.  Requires the {@link android.Manifest.permission#REBOOT}
     * permission.
     *
     * @param reason code to pass to the kernel (e.g., "recovery") to
     *               request special boot modes, or null.
     */
    public void reboot(String reason)
    {  
        try {
            mService.reboot(reason);
        } catch (RemoteException e) {
        }  
    }
mService为IPowerManager Binder接口服务。


[java] 
/**
 * {@hide}
 */ 
public PowerManager(IPowerManager service, Handler handler) 

    mService = service; 
    mHandler = handler; 

    /**
     * {@hide}
     */
    public PowerManager(IPowerManager service, Handler handler)
    {
        mService = service;
        mHandler = handler;
    }


2.frameworks/base/core/java/android/os/IPowerManager.aidl

[java]
inte易做图ce IPowerManager 

... 
void reboot(String reason); 
... 

inte易做图ce IPowerManager
{
...
void reboot(String reason);
...
}

3.frameworks/base/services/java/com/android/server/PowerManagerService.java

[java] 
/**  
 * Reboot the device immediately, passing 'reason' (may be null)
 * to the underlying __reboot system call.  Should not return.
 */ 
public void reboot(String reason) 
{     
    mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); 
 
    if (mHandler == null || !ActivityManagerNative.isSystemReady()) { 
        throw new IllegalStateException("Too early to call reboot()"); 
    }     
 
    final String finalReason = reason; 
    Runnable runnable = new Runnable() { 
        public void run() { 
            synchronized (this) { 
                ShutdownThread.reboot(getUiContext(), finalReason, false); 
            }     
 
        }     
    };    
    // ShutdownThread must run on a looper capable of displaying the UI.  
    mHandler.post(runnable); 
 
    // PowerManager.reboot() is documented not to return so just wait for the inevitable.  
    synchronized (runnable) { 
        while (true) { 
            try { 
                runnable.wait(); 
            } catch (InterruptedException e) {  
            }     
        }     
    }     

    /** 
     * Reboot the device immediately, passing 'reason' (may be null)
     * to the underlying __reboot system call.  Should not return.
     */
    public void reboot(String reason)
    {   
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);

        if (mHandler == null || !ActivityManagerNative.isSystemReady()) {
            throw new IllegalStateException("Too early to call reboot()");
        }   

        final String finalReason = reason;
        Runnable runnable = new Runnable() {
            public void run() {
                synchronized (this) {
                    ShutdownThread.reboot(getUiContext(), finalReason, false);
                }   

            }   
        };  
        // ShutdownThread must run on a looper capable of displaying the UI.
        mHandler.post(runnable);

        // PowerManager.reboot() is documented not to return so just wait for the inevitable.
        synchronized (runnable) {
   &

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,