今日想用开发一款手机用户操作,智能提示用户操作的软件,想到要服务端可能要启动很多线程,就想起androi消息机制,于是上网收了下:
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestDriver {
ThreadPoolManager tpm = ThreadPoolManager.newInstance();
public void sendMsg(String msg) {
tpm.addLogMsg(msg + "记录一条日志 ");
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new TestDriver().sendMsg(Integer.toString(i));
}
}
}
class ThreadPoolManager {
private static ThreadPoolManager tpm = new ThreadPoolManager();
// 线程池维护线程的最少数量
private final static int CORE_POOL_SIZE = 4;
// 线程池维护线程的最大数量
private final static int MAX_POOL_SIZE = 10;
// 线程池维护线程所允许的空闲时间
private final static int KEEP_ALIVE_TIME = 0;
// 线程池所使用的缓冲队列大小
private final static int WORK_QUEUE_SIZE = 10;
// 消息缓冲队列
Queue msgQueue = new LinkedList();
// 访问消息缓存的调度线程
final Runnable accessBufferThread = new Runnable() {
public void run() {
// 查看是否有待定请求,如果有,则创建一个新的AccessDBThread,并添加到线程池中
if (hasMoreAcquire()) {
String msg = (String) msgQueue.poll();
Runnable task = new AccessDBThread(msg);
threadPool.execute(task);
}
}
};
final RejectedExecutionHandler handler = new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){
System.out.println(((AccessDBThread) r).getMsg() + "消息放入队列中重新等待执行");
msgQueue.offer(((AccessDBThread) r).getMsg());
}
};
// 管理数据库访问的线程池
final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,new ArrayBlockingQueue(WORK_QUEUE_SIZE), this.handler);
// 调度线程池
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(accessBufferThread, 0, 1, TimeUnit.SECONDS);
public static ThreadPoolManager newInstance() {
return tpm;
}
private ThreadPoolManager() {
}
private boolean hasMoreAcquire() {
return !msgQueue.isEmpty();
}
public void addLogMsg(String msg) {
Runnable task = new AccessDBThread(msg);
threadPool.execute(task);
}
}
class AccessDBThread implements Runnable {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public AccessDBThread() {
super();
}
public AccessDBThread(String msg) {
this.msg = msg;
}
public void run()
{
// 向数据库中添加Msg变量值
System.out.println("Added the message: " + msg + " into the Database");
}
}
运行结果:
Added the message: 0记录一条日志 into the Database
Added the message: 2记录一条日志 into the Database
Added the message: 5记录一条日志 into the Database
Added the message: 1记录一条日志 into the Database
Added the message: 6记录一条日志 into the Database
Added the message: 8记录一条日志 into the Database
Added the message: 9记录一条日志 into the Database
Added the message: 10记录一条日志 into the Database
Added the message: 11记录一条日志 into the Database
Added the message: 21记录一条日志 into the Database
Added the message: 13记录一条日志 into the Database
Added the message: 14记录一条日志 into the Database
31记录一条日志 消息放入队列中重新等待执行
Added the message: 3记录一条日志 into the Database
Added the message: 26记录一条日志 into the Database
Added the message: 19记录一条日志 into the Database
Added the message: 4记录一条日志 into the Database
Added the message: 20记录一条日志 into the Database
Added the message: 24记录一条日志 into the Database
33记录一条日志 消息放入队列中重新等待执行
Added the message: 23记录一条日志 into the Database
Added the message: 27记录一条日志 into the Database
Added the message: 29记录一条日志 into the Database
Added the message: 30记录一条日志 into the Database
Added the message: 28记录一条日志 into the Database
Added the message: 34记录一条日志 into the Database
.....
通过.excute执行线程,可调运行数量等。
2.获取android手机topActivity及其信息
am = (ActivityManager) mContext.getSystemService(
"activity"
);
ComponentName cn = am.getRunningTasks(
1
).get(
0
).topActivity;
String packageName = cn.getPackageName();
String className = cn.getClassName();
android截图,和adb截图(adb shell /system/bin/screencap -p /sdcard/screenshot.png)原理一样
Runtime run = Runtime.getRuntime();
Process process = run.exec("su");
DataOutputStream dos = new DataOutputStream(process.getOutputStream());
String command = " /system/bin/screencap -p /sdcard/screenshot.png ";
dos.writeBytes(command + "\n");
dos.flush();
dos.writeBytes("exit \n");
dos.flush();