Ubuntu系统下Android4.0源码开发多寄存器变量路口信号灯控制系统(课程设计)
前言
Ubuntu系统版本:12.04
Android源码文件夹名:aosp
Goldfish内核文件夹名:goldfish
项目工程名:Tslights
本项目使用多寄存器变量存取
程序运行效果:
1.1 底层驱动程序代码编写
文件结构
~/goldfish2.6.29/drivers
-----tslights.h
-----tslights.c
-----Kconfig
-----Makefile
tslights.h
#ifndef _FAKE_REG_H_#define _FAKE_REG_H_#include #include #define TSLIGHTS_DEVICE_NODE_NAME "tslights"#define TSLIGHTS_DEVICE_FILE_NAME "tslights"#define TSLIGHTS_DEVICE_PROC_NAME "tslights"#define TSLIGHTS_DEVICE_CLASS_NAME "tslights"struct tslights_dev {int red_init;//红灯状态值int red_time;//红灯时间int green_init;int green_time;int yellow_init;int yellow_time;};#endif
tslights.c
#include #include #include #include #include #include #include #include "tslights.h"static int tslights_major = 0;static int tslights_minor = 0;static struct cdev rdev;// 定义字符设备static struct class* tslights_class = NULL;static struct tslights_dev* tslightsdev = NULL;static char tslights_buff[256];static ssize_t tslights_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) {copy_to_user((struct tslights_dev __user *)buf, tslightsdev, sizeof(struct tslights_dev));return 0;}static ssize_t tslights_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) {copy_from_user(tslightsdev, (struct tslights_dev __user *)buf, count);return 0;}static struct file_operations tslights_ops = { .owner = THIS_MODULE,.read = tslights_read, .write = tslights_write,};static ssize_t tslights_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) {if(off > 0) {*eof = 1;return 0;}int len;int* red_init = (int*)kmalloc(64, GFP_KERNEL);int* red_time = (int*)kmalloc(64, GFP_KERNEL);int* green_init = (int*)kmalloc(64, GFP_KERNEL);int* green_time = (int*)kmalloc(64, GFP_KERNEL);int* yellow_init = (int*)kmalloc(64, GFP_KERNEL);int* yellow_time = (int*)kmalloc(64, GFP_KERNEL);sprintf(red_init, "%d", tslightsdev->red_init);sprintf(red_time, "%d", tslightsdev->red_time);sprintf(green_init, "%d", tslightsdev->green_init);sprintf(green_time, "%d", tslightsdev->green_time);sprintf(yellow_init, "%d", tslightsdev->yellow_init);sprintf(yellow_time, "%d", tslightsdev->yellow_time);memset(tslights_buff, 0, 256);strcat(tslights_buff,"red state:");strcat(tslights_buff,red_init);strcat(tslights_buff," red time:");strcat(tslights_buff,red_time);strcat(tslights_buff,"green state:");strcat(tslights_buff,green_init);strcat(tslights_buff," green time:");strcat(tslights_buff,green_time);strcat(tslights_buff,"yellow state:");strcat(tslights_buff,yellow_init);strcat(tslights_buff," yellow time:");strcat(tslights_buff,yellow_time);kfree(red_init);kfree(red_time);kfree(green_init);kfree(green_time);kfree(yellow_init);kfree(yellow_time);len=sprintf(page,tslights_buff);return len;}static ssize_t tslights_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data) {return 0;}static void tslights_create_proc(void) {struct proc_dir_entry* entry;entry = create_proc_entry(TSLIGHTS_DEVICE_PROC_NAME, 0, NULL);if(entry) {entry->owner = THIS_MODULE;entry->read_proc = tslights_proc_read;entry->write_proc = tslights_proc_write;}}static void tslights_remove_proc(void) {remove_proc_entry(TSLIGHTS_DEVICE_PROC_NAME, NULL);}// 初始化模块时操作static int __init tslights_init(void){int result;dev_t dev = 0;tslightsdev = (struct tslights_dev*) kmalloc(sizeof(struct tslights_dev), GFP_KERNEL);alloc_chrdev_region(&dev, 0, 1, TSLIGHTS_DEVICE_NODE_NAME);tslights_major = MAJOR(dev);tslights_minor = MINOR(dev);cdev_init(&rdev, &tslights_ops);result = cdev_add(&rdev, dev, 1);tslights_create_proc();tslightsdev->green_init=1;tslightsdev->yellow_init=0;tslightsdev->red_init = 0;tslightsdev->green_time=10;tslightsdev->yellow_time=3;tslightsdev->red_time=8;tslights_class = class_create(THIS_MODULE, TSLIGHTS_DEVICE_CLASS_NAME);struct device* RCtemp = device_create(tslights_class, NULL, dev, "%s", TSLIGHTS_DEVICE_FILE_NAME);// 创建一个字符结点文件if (result < 0) goto add_err;return 0;add_err: unregister_chrdev_region(dev, 1); return -1;}// 卸载模块时操作static void __exit tslights_exit(void){ dev_t dev = MKDEV(tslights_major, tslights_minor);tslights_remove_proc(); cdev_del(&rdev);kfree(tslightsdev); unregister_chrdev_region(dev, 1);}MODULE_LICENSE("GPL");MODULE_DESCRIPTION("TSLights Register Driver");module_init(tslights_init);module_exit(tslights_exit);
Kconfig
config TSLIGHTStristate "Tslights Register Driver"default nhelpThis is the tslights driver for android system.
Makefile
obj-$(CONFIG_TSLIGHTS) += tslights.o
1.2 修改内核Kconfig文件
打开arch/arm/Kconfig文件
在这两行之间
menu "Device Drivers"
.....
endmenu
插入 source "drivers/tslgihts/Kconfig"
1.3 修改内核Makefile文件
打开drivers/Makefile文件,在里面添加以下一行内容:
......
obj-$(CONFIG_TSLIGHTS)+=tslights/
1.4 编译内核驱动程序模块
在goldfish目录下执行 make menuconfig命令来配置它的编译方式
选择 "Device Drivers" 回车
选择 Fake Register Driver项按Y键
配置完成后保存编译配置,退出make menuconfig命令。
接着执行 ./build_kernel.sh命令
1.5 编写硬件抽象层模块接口
文件结构:
~/aosp/hardware/libhardware
-----include/hardware
tslights.h
-----modules/tslights
tslights.cpp
android.mk
tslights.h
#ifndef ANDROID_TSLIGHTS_INTERFACE_H#define ANDROID_TSLIGHTS_INTERFACE_H#include __BEGIN_DECLS/** * The id of this module */#define TSLIGHTS_HARDWARE_MODULE_ID "tslights"/** * The id of this device */#define TSLIGHTS_HARDWARE_DEVICE_ID "tslights"struct tslights_module_t {struct hw_module_t common;};struct tslights_dev {int red_init;int red_time;int green_init;int green_time;int yellow_init;int yellow_time;};struct tslights_device_t {struct hw_device_t common;int fd;int (*set_tslightsdev)(struct tslights_device_t* dev,struct tslights_dev tslightsdev);int (*get_tslightsdev)(struct tslights_device_t* dev,struct tslights_dev* tslightsdev);};__END_DECLS#endif
tslights.cpp
#define LOG_TAG "tslightsHALStub"#include #include #include #include #include #include #define DEVICE_NAME "/dev/tslights"#define MODULE_NAME "tslights"#define MODULE_AUTHOR "Clear_Zhang"static int tslights_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device);static int tslights_device_close(struct hw_device_t* device);static int tslights_set_tslightsdev(struct tslights_device_t* dev,struct tslights_dev tslightsdev);static int tslights_get_tslightsdev(struct tslights_device_t* dev,struct tslights_dev* tslightsdev);static struct hw_module_methods_t tslights_module_methods = {open: tslights_device_open};struct tslights_module_t HAL_MODULE_INFO_SYM = {common: {tag: HARDWARE_MODULE_TAG,version_major: 1,version_minor: 0,id: TSLIGHTS_HARDWARE_MODULE_ID,name: MODULE_NAME,author: MODULE_AUTHOR,methods: &tslights_module_methods,}};static int tslights_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device) {if(!strcmp(id, TSLIGHTS_HARDWARE_DEVICE_ID)) {struct tslights_device_t* dev;struct tslights_dev* tslightsdev = (struct tslights_dev*)malloc(sizeof(struct tslights_dev));dev = (struct tslights_device_t*)malloc(sizeof(struct tslights_device_t));if(!dev) {LOGE("Failed to alloc space for tslights_device_t.");return -EFAULT;}memset(dev, 0, sizeof(struct tslights_device_t));dev->common.tag = HARDWARE_DEVICE_TAG;dev->common.version = 0;dev->common.module = (hw_module_t*)module;dev->common.close = tslights_device_close;dev->set_tslightsdev = tslights_set_tslightsdev;dev->get_tslightsdev = tslights_get_tslightsdev;if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {LOGE("Failed to open device file /dev/tslights -- %s.", strerror(errno));free(dev);return -EFAULT;}*device = &(dev->common);LOGI("Open device file /dev/tslights successfully.");return 0;}return -EFAULT;}static int tslights_device_close(struct hw_device_t* device) {struct tslights_device_t* tslights_device = (struct tslights_device_t*)device;if(tslights_device) {close(tslights_device->fd);free(tslights_device);}return 0;}static int tslights_set_tslightsdev(struct tslights_device_t* dev, struct tslights_dev tslightsdev) {if(!dev) {LOGE("Null dev pointer.");return -EFAULT;}LOGI("Set value %d to device file /dev/tslights.", tslightsdev.red_init);write(dev->fd, &tslightsdev, sizeof(struct tslights_dev));return 0;}static int tslights_get_tslightsdev(struct tslights_device_t* dev,struct tslights_dev* tslightsdev) {if(!dev) {LOGE("Null dev pointer.");return -EFAULT;}if(!tslightsdev) {LOGE("Null val pointer.");return -EFAULT;}read(dev->fd, tslightsdev, sizeof(struct tslights_dev));LOGI("Get value %d from device file /dev/tslights.", tslightsdev->red_init);return 0;}
Android.mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_PRELINK_MODULE := falseLOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hwLOCAL_SHARED_LIBRARIES := liblogLOCAL_SRC_FILES := tslights.cppLOCAL_MODULE := tslights.defaultinclude $(BUILD_SHARED_LIBRARY)
执行mmm和make snod命令对其进行编译和打包
1.6 处理硬件设备访问权限问题
1、解压ramdisk.img镜像文件
2、还原ramdisk.img镜像文件
3、修改ueventd.rc文件
进入ramdisk目录中,打开ueventd.rc文件,向文件里添加以下内容:
/dev/tslights 0666 root root
4、重新打包ramdisk.img镜像文件
1.7 定义硬件访问服务接口
文件结构:
~/aosp/frameworks/base/core/java/android/os
-----ITslightsService.aidl
ITslightsService.aidl
package android.os;interface ITslightsService {void setRed_init(int red_init);int getRed_init();void setRed_time(int red_time);int getRed_time();void setGreen_init(int green_init);int getGreen_init();void setGreen_time(int green_time);int getGreen_time();void setYellow_init(int yellow_init);int getYellow_init();void setYellow_time(int yellow_time);int getYellow_time();}
进入到frameworks/base目录中,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值
添加 core/java/android/os/ITslightsService.aidl
编译硬件访问服务接口
1.8 实现硬件访问服务
文件结构:
~/aosp/frameworks/base/service/java/com/anroid/server
-----TslightsService.java
TslightsService.java
package com.android.server;import android.content.Context;import android.os.ITslightsService;import android.util.Slog;public class TslightsService extends ITslightsService.Stub {private static final String TAG = "TslightsService";private int mPtr = 0;TslightsService() {mPtr = init_native();if(mPtr == 0) {Slog.e(TAG, "Failed to initialize tslights service.");}}public void setRed_init(int red_init) {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return;}setRed_init_native(mPtr, red_init);}public int getRed_init() {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return 0;}return getRed_init_native(mPtr);}public void setRed_time(int red_time) {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return;}setRed_time_native(mPtr, red_time);}public int getRed_time() {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return 0;}return getRed_time_native(mPtr);}public void setGreen_init(int green_init) {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return;}setGreen_init_native(mPtr, green_init);}public int getGreen_init() {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return 0;}return getGreen_init_native(mPtr);}public void setGreen_time(int green_time) {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return;}setGreen_time_native(mPtr, green_time);}public int getGreen_time() {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return 0;}return getGreen_time_native(mPtr);}public void setYellow_init(int yellow_init) {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return;}setYellow_init_native(mPtr, yellow_init);}public int getYellow_init() {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return 0;}return getYellow_init_native(mPtr);}public void setYellow_time(int yellow_time) {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return;}setYellow_time_native(mPtr, yellow_time);}public int getYellow_time() {if(mPtr == 0) {Slog.e(TAG, "Tslights service is not initialized.");return 0;}return getYellow_time_native(mPtr);}private static native int init_native();private static native void setRed_init_native(int ptr, int red_init);private static native int getRed_init_native(int ptr);private static native void setRed_time_native(int ptr, int red_time);private static native int getRed_time_native(int ptr);private static native void setGreen_init_native(int ptr, int green_init);private static native int getGreen_init_native(int ptr);private static native void setGreen_time_native(int ptr, int green_time);private static native int getGreen_time_native(int ptr);private static native void setYellow_init_native(int ptr, int yellow_init);private static native int getYellow_init_native(int ptr);private static native void setYellow_time_native(int ptr, int yellow_time);private static native int getYellow_time_native(int ptr);};
执行mmm命令编译services模块
1.9 实现硬件访问服务的JNI方法
文件结构:
~/aosp/frameworks/base/service/jni/
-----com_android_server_TslightsService.cpp
com_android_server_TslightsService.cpp
#define LOG_TAG "TslightsServiceJNI"#include "jni.h"#include "JNIHelp.h"#include "android_runtime/AndroidRuntime.h"#include #include #include #include #include struct tslights_dev tslightsdev;namespace android{static void tslights_setRed_init(JNIEnv* env, jobject clazz, jint ptr, jint value) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return;}tslightsdev.red_init = value;LOGI("Set value %d to device tslights.", tslightsdev.red_init);device->set_tslightsdev(device, tslightsdev);}static jint tslights_getRed_init(JNIEnv* env, jobject clazz, jint ptr) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return 0;}device->get_tslightsdev(device, &tslightsdev);LOGI("Get value %d from device tslights.", tslightsdev.red_init);return tslightsdev.red_init;}///////////////////////////////////////////////////////////////////////////////////static void tslights_setRed_time(JNIEnv* env, jobject clazz, jint ptr, jint value) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return;}tslightsdev.red_time = value;LOGI("Set value %d to device tslights.", tslightsdev.red_time);device->set_tslightsdev(device, tslightsdev);}static jint tslights_getRed_time(JNIEnv* env, jobject clazz, jint ptr) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return 0;}device->get_tslightsdev(device, &tslightsdev);LOGI("Get value %d from device tslights.", tslightsdev.red_time);return tslightsdev.red_time;}///////////////////////////////////////////////////////////////////////////////////static void tslights_setGreen_init(JNIEnv* env, jobject clazz, jint ptr, jint value) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return;}tslightsdev.green_init = value;LOGI("Set value %d to device tslights.", tslightsdev.green_init);device->set_tslightsdev(device, tslightsdev);}static jint tslights_getGreen_init(JNIEnv* env, jobject clazz, jint ptr) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return 0;}device->get_tslightsdev(device, &tslightsdev);LOGI("Get value %d from device tslights.", tslightsdev.green_init);return tslightsdev.green_init;}///////////////////////////////////////////////////////////////////////////////////static void tslights_setGreen_time(JNIEnv* env, jobject clazz, jint ptr, jint value) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return;}tslightsdev.green_time = value;LOGI("Set value %d to device tslights.", tslightsdev.green_time);device->set_tslightsdev(device, tslightsdev);}static jint tslights_getGreen_time(JNIEnv* env, jobject clazz, jint ptr) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return 0;}device->get_tslightsdev(device, &tslightsdev);LOGI("Get value %d from device tslights.", tslightsdev.green_time);return tslightsdev.green_time;}///////////////////////////////////////////////////////////////////////////////////static void tslights_setYellow_init(JNIEnv* env, jobject clazz, jint ptr, jint value) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return;}tslightsdev.yellow_init = value;LOGI("Set value %d to device tslights.", tslightsdev.yellow_init);device->set_tslightsdev(device, tslightsdev);}static jint tslights_getYellow_init(JNIEnv* env, jobject clazz, jint ptr) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return 0;}device->get_tslightsdev(device, &tslightsdev);LOGI("Get value %d from device tslights.", tslightsdev.yellow_init);return tslightsdev.yellow_init;}///////////////////////////////////////////////////////////////////////////////////static void tslights_setYellow_time(JNIEnv* env, jobject clazz, jint ptr, jint value) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return;}tslightsdev.yellow_time = value;LOGI("Set value %d to device tslights.", tslightsdev.yellow_time);device->set_tslightsdev(device, tslightsdev);}static jint tslights_getYellow_time(JNIEnv* env, jobject clazz, jint ptr) {tslights_device_t* device = (tslights_device_t*)ptr;if(!device) {LOGE("Device tslights is not open.");return 0;}device->get_tslightsdev(device, &tslightsdev);LOGI("Get value %d from device tslights.", tslightsdev.yellow_time);return tslightsdev.yellow_time;}static inline int tslights_device_open(const hw_module_t* module, struct tslights_device_t** device) {return module->methods->open(module, TSLIGHTS_HARDWARE_DEVICE_ID, (struct hw_device_t**)device);}static jint tslights_init(JNIEnv* env, jclass clazz) {tslights_module_t* module;tslights_device_t* device;LOGI("Initializing HAL stub tslights......");if(hw_get_module(TSLIGHTS_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) {LOGI("Device tslights found.");if(tslights_device_open(&(module->common), &device) == 0) {LOGI("Device tslights is open.");return (jint)device;}LOGE("Failed to open device tslights.");return 0;}LOGE("Failed to get HAL stub tslights.");return 0;}static const JNINativeMethod method_table[] = {{"init_native", "()I", (void*)tslights_init},{"setRed_init_native", "(II)V", (void*)tslights_setRed_init},{"getRed_init_native", "(I)I", (void*)tslights_getRed_init},{"setRed_time_native", "(II)V", (void*)tslights_setRed_time},{"getRed_time_native", "(I)I", (void*)tslights_getRed_time},{"setGreen_init_native", "(II)V", (void*)tslights_setGreen_init},{"getGreen_init_native", "(I)I", (void*)tslights_getGreen_init},{"setGreen_time_native", "(II)V", (void*)tslights_setGreen_time},{"getGreen_time_native", "(I)I", (void*)tslights_getGreen_time},{"setYellow_init_native", "(II)V", (void*)tslights_setYellow_init},{"getYellow_init_native", "(I)I", (void*)tslights_getYellow_init},{"setYellow_time_native", "(II)V", (void*)tslights_setYellow_time},{"getYellow_time_native", "(I)I", (void*)tslights_getYellow_time},};int register_android_server_TslightsService(JNIEnv *env) { return jniRegisterNativeMethods(env, "com/android/server/TslightsService", method_table, NELEM(method_table));}};
进入到frameworks/base/services/jni目录中,打开Android.mk文件,修改变量LOCAL_SRC_FILES的值
LOCAL_SRC_FILES += \
......
com_android_server_TslightsService.cpp/
onload.cpp
修改完成后,执行mmm命令来重新编译libandroid_servers模块
1.10 启动硬件访问服务
进入到frameworks/base/services/java/com/android/server目录中,打开SystemServer.java文件,修改ServerThread类的成员函数run的实现:
try {
Slog.i(TAG, "Tslights Service");
ServiceManager.addService("tslights", new TslightsService());
} catch (Throwable e) {
Slog.e(TAG, "Failure starting Tslights Service", e);
}
最后,执行mmm和make snod命令
2.1开发Android应用程序
文件结构:
~/aosp/packages/experimental/Tslights
-----AndroidManifest.xml
-----Android.mk
-----src
-----clear/zhang/tslights
Tslights.java
-----res
-----layout
Main..xml
-----drewable
icon.png
pred.jpg
pgreen.jpg
pyellow.jpg
-----values
Strings.xml
Tslights.java
package clear.zhang.tslights;import android.app.Activity;import android.os.ServiceManager;import android.os.Bundle;import android.os.ITslightsService;import android.os.RemoteException;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.os.Handler;import android.widget.LinearLayout;import android.widget.ImageView;import android.os.Message;import android.os.CountDownTimer;public class Tslights extends Activity {private final static String LOG_TAG = "clear.zhang.tslights.TslightsActivity";private ITslightsService tslightsService = null;private TextView viewById=null;int textnum = 60;private ImageView imageViewred; private ImageView imageViewgreen; private ImageView imageViewyellow; Handler handler = new Handler(); int i=0; int red_time=8; int red_init =1 ; int green_time=10; int green_init =1; int yellow_time=3; int yellow_init =1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);tslightsService = ITslightsService.Stub.asInterface(ServiceManager.getService("tslights")); viewById = (TextView)findViewById(R.id.tv_count);imageViewred=(ImageView)findViewById(R.id.red); imageViewgreen=(ImageView)findViewById(R.id.green); imageViewyellow=(ImageView)findViewById(R.id.yellow); viewById.setText(String.valueOf(textnum));imageViewred.setImageDrawable(getResources().getDrawable(R.drawable.pred)); imageViewgreen.setImageDrawable(getResources().getDrawable(R.drawable.pgreen)); imageViewyellow.setImageDrawable(getResources().getDrawable(R.drawable.pyellow));try { red_init=tslightsService.getRed_init();red_time=tslightsService.getRed_time();green_init=tslightsService.getGreen_init();green_time=tslightsService.getGreen_time();yellow_init=tslightsService.getYellow_init();yellow_time=tslightsService.getYellow_time();} catch (RemoteException e) {Log.e(LOG_TAG, "Remote Exception while reading value from freg service.");} Log.i(LOG_TAG, "Tslights Activity Created"); handler.postDelayed(runnable,1000);}Runnable runnable = new Runnable() { @Override public void run() { i++; if (i >= 0 && i <= green_time&&green_init==1) { imageViewred.setVisibility(View.INVISIBLE); imageViewgreen.setVisibility(View.VISIBLE); imageViewyellow.setVisibility(View.INVISIBLE); CountDownTimer timer1=new CountDownTimer(green_time*1000+500, 1000) { public void onTick(long millisUntilFinished) { int a= (int) (millisUntilFinished/1000); viewById.setText(String.valueOf(a)); //viewById.setText(String.valueOf(millisUntilFinished / 1000)); } public void onFinish() { //LogUtil.i(TAG, "done!"); } }.start();try { green_init=0;tslightsService.setGreen_init(green_init); yellow_init=1;tslightsService.setYellow_init(yellow_init);} catch (RemoteException e) {Log.e(LOG_TAG, "Remote Exception while reading value from freg service.");} } else if (i >= green_time+1 && i <= green_time+yellow_time&&yellow_init==1) { imageViewred.setVisibility(View.INVISIBLE); imageViewgreen.setVisibility(View.INVISIBLE); imageViewyellow.setVisibility(View.VISIBLE); CountDownTimer timer2=new CountDownTimer(yellow_time*1000+500, 1000) { public void onTick(long millisUntilFinished) { int a= (int) (millisUntilFinished/1000); viewById.setText(String.valueOf(a)); } public void onFinish() { //LogUtil.i(TAG, "done!"); } }.start();try { yellow_init=0;tslightsService.setYellow_init(yellow_init); red_init=1;tslightsService.setRed_init(red_init);} catch (RemoteException e) {Log.e(LOG_TAG, "Remote Exception while reading value from freg service.");} } else if (i >= green_time+yellow_time+1 && i <= red_time+green_time+yellow_time&&red_init==1) { imageViewred.setVisibility(View.VISIBLE); imageViewgreen.setVisibility(View.INVISIBLE); imageViewyellow.setVisibility(View.INVISIBLE); CountDownTimer timer3=new CountDownTimer(red_time*1000+500, 1000) { public void onTick(long millisUntilFinished) { int a= (int) (millisUntilFinished/1000); viewById.setText(String.valueOf(a)); } public void onFinish() { //LogUtil.i(TAG, "done!"); } }.start();try { red_init=0;tslightsService.setRed_init(red_init); green_init=1;tslightsService.setGreen_init(green_init);} catch (RemoteException e) {Log.e(LOG_TAG, "Remote Exception while reading value from freg service.");} } else if (i > red_time+green_time+yellow_time) { i = 0; } handler.postDelayed(this, 1000); } }; }
main.xml
<?xml version="1.0" encoding="utf-8"?>
各文件编写好后,进行编译和打包
最后,启动模拟器
注:若启动模拟器发现一直停留在开机动画页面的话,请执行make命令后,再重新启动模拟器。
附源码包链接:https://download.csdn.net/download/qq_40789901/12667732
更多相关文章
- Android(安卓)官方屏幕适配之ScreenMatch
- web端、android端的文件上传
- ubuntu 下 Android开 发 环境搭建
- 使用广播实现的Android关机及重启
- Android(安卓)获取系统或SDCARD剩余空间信息
- android SQLite3常用命令&语法
- toast的五种效果
- 如何在windows 下运行android 4.0 ICS CTS
- Android(安卓)漂亮的 Dialog (弹出框)