说明
在 Android 中, 所有的系统服务(AMS, PMS, WMS)都是采用 C / S 进行 IPC 远程调用的
// 例如:
ActivityManagerService 需要通过 ActivityManager 进行调用.
PackageManagerService 需要通过 PakcageManager 进行调用.
WindowsManagerService 需要通过 WindowsManager 进行调用.
当系统层面添加 API 接口时, 需要更新对应的 class/jar 文件给到应用, 问题是什么?
jar包文件通常在 IDE(Android Studio) 里面已经封装好了:
- 如果把 class.jar 做 Unzip 修改会存在:
- 个别 class 方法重名
- API 版本不一致(例如 21 对应的是 4.4, 23 对应的是 6.0)
设计模式
- aidl
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.helper;
oneway interface IPowerEventListener {
void onBacklightChange(boolean on);
}
- singleton class
package android.helper;
import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.content.Context;
import android.util.Log;
import android.os.PowerManager;
import android.helper.IPowerEventListener;
public final class PowerManagerHelper extends Helper {
private static final String TAG = "PowerManagerHelper";
private final PowerManager mPowerManager;
public PowerManagerHelper(Context context) {
super(context);
mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
}
private static PowerManagerHelper _this = null;
public static PowerManagerHelper getInstance(Context context) {
if(null == _this) {
synchronized(PowerManagerHelper.class) {
try {
if(null == _this) {
_this = new PowerManagerHelper(context);
}
} catch (Exception ex) {
Log.e(TAG, "PowerManagerHelper getInstance error.", ex);
}
}
}
return _this;
}
public void setForceShutdownTime(long delayMillis) {
// do thing
}
public void setNoOpsShutdownTime(long delayMillis) {
// do thing
}
public void setPowerEventListener(IPowerEventListener listener) {
// mPowerManager.setPowerEventListener(listener);
}
}
如何打包
Android SDK编译出来在: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/
拷贝 class 到路径其他地方, 使用 jar 命令: jar cvfm system-custom.jar ./classdir