在/system/bin/下的可执行文件可以通过adb shell 运行,那怎么实现开机自己启动呢
一、 首先先仿照bootanimation写一个可以悬浮在屏幕上的图形
-
在frameworks/base/cmds/下新建你自己文件夹(在这儿我起名为mytestdemo),文件夹下存放Android.bp,xxx.cpp(MyTestDemo.cpp)
-
MyTestDemo.cpp
通过新增一个Layer,在这边不多讲解
#define LOG_TAG "SurfaceTestDemo"
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>
#include <gui/ISurfaceComposerClient.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/SurfaceControl.h>
#include <hardware/gralloc.h>
#include <sys/resource.h>
#include <system/window.h>
#include <utils/Log.h>
#include <utils/RefBase.h>
using namespace android;
void fillRGBA8Buffer(uint8_t* img, int width, int height, int stride, int r, int g, int b) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
uint8_t* pixel = img + (4 * (y * stride + x));
pixel[0] = r;
pixel[1] = g;
pixel[2] = b;
pixel[3] = 255;
}
}
}
int MyTestDemo() {
status_t err = NO_ERROR;
int countFrame = 0;
ANativeWindow_Buffer nativeBuffer;
sp<ProcessState> proc(ProcessState::self());
ProcessState::self()->startThreadPool();
sp<SurfaceComposerClient> surfaceComposerClient = new SurfaceComposerClient();
err = surfaceComposerClient->initCheck();
if (err != NO_ERROR) {
ALOGD("SurfaceComposerClient::initCheck error: %#x\n", err);
return -1;
}
// Get main display parameters.
sp<IBinder> displayToken = SurfaceComposerClient::getInternalDisplayToken();
if (displayToken == nullptr) return -1;
ui::DisplayMode displayMode;
const status_t error = SurfaceComposerClient::getActiveDisplayMode(displayToken, &displayMode);
if (error != NO_ERROR) return -1;
ui::Size resolution(200, 200);
// create the surface
sp<SurfaceControl> surfaceControl =
surfaceComposerClient->createSurface(String8("SurfaceTestDemo"), resolution.getWidth(),
resolution.getHeight(), PIXEL_FORMAT_RGBA_8888,
ISurfaceComposerClient::eFXSurfaceBufferState,
/*parent*/ nullptr);
SurfaceComposerClient::Transaction{}
.setLayer(surfaceControl, std::numeric_limits<int32_t>::max())
.setPosition(surfaceControl, (displayMode.resolution.getWidth() - 200) / 2,
(displayMode.resolution.getHeight() - 200) / 2)
.show(surfaceControl)
.apply();
int mWidth = resolution.getWidth();
int mHeight = resolution.getHeight();
sp<Surface> surface = surfaceControl->getSurface();
while (1) {
countFrame = (countFrame + 1) % 3;
surface->lock(&nativeBuffer, NULL);
fillRGBA8Buffer((uint8_t*)nativeBuffer.bits, mWidth, mHeight, nativeBuffer.stride,
countFrame == 0 ? 255 : 0, countFrame == 1 ? 255 : 0,
countFrame == 2 ? 255 : 0);
surface->unlockAndPost();
sleep(1);
}
return err;
}
int main() {
setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_DISPLAY);
sp<ProcessState> proc(ProcessState::self());
ProcessState::self()->startThreadPool();
MyTestDemo();
IPCThreadState::self()->joinThreadPool();
return 0;
}
- Android.bp
package {
// See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import
// all of the 'license_kinds' from "frameworks_base_license"
// to get the below license kinds:
// SPDX-license-identifier-Apache-2.0
default_applicable_licenses: ["frameworks_base_license"],
}
cc_binary {
name: "mytestdemo",
header_libs: ["jni_headers"],
shared_libs: [
"libbase",
"libbinder",
"libcutils",
"libEGL",
"libfmq",
"libGLESv1_CM",
"libGLESv2",
"libgui",
"libhidlbase",
"liblayers_proto",
"liblog",
"libnativewindow",
"libprocessgroup",
"libprotobuf-cpp-lite",
"libsync",
"libtimestats",
"libui",
"libinput",
"libutils",
"libSurfaceFlingerProp",
"librkgfx_dc",
],
srcs: [
"MyTestDemo.cpp",
],
cflags: [
"-Wno-deprecated-declarations",
],
}
二、 开机自动启动这个services
- 在system/core/rootdir/init.rc中添加
service mytest /system/bin/mytestdemo
class core animation
user graphics
group graphics audio
- 在system/sepolicy/private/下与system/sepolicy/prebuilts/api/你自己的版本/private/两个目录下都要新建mytest.te
mytest.te
# wzxtest
type mytest, domain, coredomain;
type mytest_exec, exec_type, file_type, system_file_type;
init_daemon_domain(mytest)
- 在system/sepolicy/private/file_contexts下与system/sepolicy/prebuilts/api/你自己的版本/private/file_contexts 新增
/system/bin/mytestdemo u:object_r:mytest_exec:s0
三、 编译替换
- 首先编译出super.img。根据编译手册在根目录下
./build.sh -A
编译出后烧录。烧录时根据烧录手册一定要被识别为LOADER。
2. 在frameworks/base/cmds/mytestdemo下,执行
mm
编译出的可执行文件push到/system/bin/