在android原生的关机流程里面是没有声音和 自定义动画的借口,因此为了实现这两个功能需要找到相应的地方并采用合适的方式进行处理。
在关机的ShutdownThread类里面有两个很重要的方法,一个是beginShutdownSequence,解析如下:
private static void beginShutdownSequence(Context context) {
synchronized (sIsStartedGuard) {
sIsStarted = true;
}
// throw up an indeterminate system dialog to indicate radio is
// shutting down.
//动画开始
//这里也是可以加入声音的播放流程,(这里要考虑到几点:声音与动画的同步;需要避免蓝牙等断开操作导致的声音不连续)
ProgressDialog pd = new ProgressDialog(context);
pd.setTitle(context.getText(com.android.internal.R.string.power_off));
pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
if (!context.getResources().getBoolean(
com.android.internal.R.bool.config_sf_slowBlur)) {
pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
}
pd.show();
//动画结束,需要在这里加入自定的view并进行显示。这里的view需要注意横竖屏的转化,让其显示唯一,后面的代码则是启动关机的动作。
// start the thread that initiates shutdown
sInstance.mContext = context;
sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
sInstance.mWakeLock = null;
if (sInstance.mPowerManager.isScreenOn()) {
try {
sInstance.mWakeLock = sInstance.mPowerManager.newWakeLock(
PowerManager.FULL_WAKE_LOCK, "Shutdown");
sInstance.mWakeLock.acquire();
} catch (SecurityException e) {
Log.w(TAG, "No permission to acquire wake lock", e);
sInstance.mWakeLock = null;
}
}
sInstance.mHandler = new Handler() {
};
sInstance.start();
}
第二个就是run方法,该方法决定了一个完整的关机流程所需要的时间。设置时间为最少10秒,其实该段代码在无声音和动画的情况下可以在5秒内执行完毕。如果考虑到声音就要注意了,因为很有可能产生破音(就是关机的最后一刻会产生比较尖锐的声音)
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.
actionDone();
}
};
Log.i(TAG, "Sending shutdown broadcast...");
// First send the high-level shut down broadcast.
mActionDone = false;
mContext.sendOrderedBroadcast(new Intent(Intent.ACTION_SHUTDOWN), null,
br, mHandler, 0, null, null);
//从这里开始
final long endTime = SystemClock.elapsedRealtime() + MAX_BROADCAST_TIME;
synchronized (mActionDoneSync) {
while (!mActionDone) {
long delay = endTime - SystemClock.elapsedRealtime();
if (delay <= 0) {
Log.w(TAG, "Shutdown broadcast timed out");
break;
}
try {
mActionDoneSync.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
IMountShutdownObserver observer = new IMountShutdownObserver.Stub() {
public void onShutDownComplete(int statusCode) throws RemoteException {
Log.w(TAG, "Result code " + statusCode + " from MountService.shutdown");
actionDone();
}
};
Log.i(TAG, "Shutting down MountService");
// Set initial variables and time out time.
mActionDone = false;
final long endShutTime = SystemClock.elapsedRealtime() + MAX_SHUTDOWN_WAIT_TIME;
synchronized (mActionDoneSync) {
try {
if (mount != null) {
mount.shutdown(observer);
} else {
Log.w(TAG, "MountService unavailable for shutdown");
}
} catch (Exception e) {
Log.e(TAG, "Exception during MountService shutdown", e);
}
while (!mActionDone) {
long delay = endShutTime - SystemClock.elapsedRealtime();
if (delay <= 0) {
Log.w(TAG, "Shutdown wait timed out");
break;
}
try {
mActionDoneSync.wait(delay);
} catch (InterruptedException e) {
}
}
}
if (mReboot) {
Log.i(TAG, "Rebooting, reason: " + mRebootReason);
try {
Power.reboot(mRebootReason);
} catch (Exception e) {
Log.e(TAG, "Reboot failed, will attempt shutdown instead", e);
}
} else if (SHUTDOWN_VIBRATE_MS > 0) {
// vibrate before shutting down
Vibrator vibrator = new Vibrator();
vibrator.vibrate(SHUTDOWN_VIBRATE_MS);
// vibrator is asynchronous so we need to wait to avoid shutting down too soon.
try {
Thread.sleep(SHUTDOWN_VIBRATE_MS);
} catch (InterruptedException e) {
}
}
//最后在这里一定要注意,声音是否播放完毕。如果没有完毕则要延时后再执行下面的命令,否则会产生破音。
// Shutdown power
Log.i(TAG, "Performing low-level shutdown...");
Power.shutdown();
}
run方法的整个关机过程包括上面的方法不能产生任何错误,即便是try catch处理过也不行,这样会导致机器调用reboot的命令,很无奈的!! 例如Thread.sleep就有可能产生错误,源码中就有这样的隐患。建议使用SystemClock.sleep替换,这样就可以避免机器重启的隐患。
分享到:
相关推荐
在Android源码中,与关机动画相关的代码主要位于`frameworks/base/services/core/java/com/android/server/power/`目录下的`ShutdownThread.java`文件中。这里定义了关机序列开始时的具体行为,包括展示关机画面。...
- 在关机log中检索“ShutdownThread”,检查安卓阶段的关机时间。 - 检索“jumping to kernel”,并计算从这里到停止打印log的时间。 - **优化建议**: - 分析jumping to kernel之前的时间,即init进程关闭服务的...
当用户选择“关机”选项后,系统将调用`ShutdownThread.java`中的`shutdown`方法。该方法内部调用了`beginShutdownSequence`方法,并最终触发了`bootanimationservice`的启动或停止。 - **启动/停止开关机动画**:`...
在Android系统中,实现定时关机功能涉及到对操作系统底层机制的理解和Java编程技术的应用。以下是对这个主题的详细解析: 1. **定时器基础**: 定时器在Java中通常通过`java.util.Timer`类和`java.util.TimerTask`...
- 关机过程中,Android会创建一个ShutdownThread,负责执行实际的关机操作。这个线程会逐步停止系统服务,关闭活动应用,以及断开与硬件的连接。 4. **电源管理**: - 电源管理是关机和重启的关键部分。Android的...
在`GlobalActions.java`中,`ShutdownThread`类扮演了关键角色,它包含了执行关机和重启的函数。对于关机,调用的是`ShutdownThread.shutdown(mContext, true);`这个方法。这个函数会触发一系列的系统级操作,最终...
ShutdownThread shutdownThread = new ShutdownThread(); shutdownThread.start(); ``` 其次,`Swing`库是Java用于构建GUI应用的主要工具。它提供了一系列组件,如按钮、文本框等,用于创建用户友好的界面。在实现...
1. **调用`shutdownThread`的`shutdown`方法**:当用户确认关机后,系统会调用`shutdownThread`类的`shutdown`方法来开始关机流程。这里需要注意的是,`shutdown`方法的第二个参数决定了是否显示确认关机的提示...
接着,我们创建了一个新线程`shutdownThread`,在该线程中根据操作系统类型执行相应的关机命令。为了确保程序不会立即结束,主线程进入无限循环,直到定时关机线程执行。 值得注意的是,由于Java的安全特性,直接...
在`onPress()`方法中,调用了`ShutdownThread.shutdown`方法,执行关机操作。该方法的第二个参数决定了是否需要弹出确认对话框。如果参数为`false`,则不弹出确认对话框,直接执行关机;如果参数为`true`,则弹出...
为了添加关闭监听功能,作者创建了另一个监听线程`shutdownThread`,它监听8001端口,当收到特定的关闭命令时,关闭线程池。这个线程会检查`isShutdown`标志来决定是否已经接收到关闭命令。关闭线程池时,调用`...
`reboot()`方法在此处被调用,首先检查调用者的权限,然后通过`mHandler`(一个处理UI事件的Handler)来执行`ShutdownThread`中的重启任务。`ShutdownThread`负责显示关机界面,收集日志信息,并执行实际的内核级...