定时器
Timer timer = null;
TimerTask task;
if (timer == null) {
timer = new Timer();
}
if (task == null) {
task = new TimerTask() {
@Override
public void run() {
System.out.println("run TimerTask");
}
};
}
if(timer != null && task != null )
timer.schedule(task,0,1000);
if (timer != null) {
timer.cancel();
timer = null;
}
if (task != null) {
task.cancel();
task = null;
}
线程
private Thread thread;
thread=new Thread(new Runnable() {
@Override
public void run() {
}
});
thread.start();
自动唤醒APP函数
PackageManager packageManager = getPackageManager();
if (PackageUtil.checkPackInfo(this,ConstantUtil.PACKAGE_NAME)) {
Intent intent = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.CUPCAKE) {
intent = packageManager.getLaunchIntentForPackage(ConstantUtil.PACKAGE_NAME);
System.out.println("MainActivity OnCreate() not start aodingPlayer");
startActivity(intent);
}
} else {
Toast.makeText(MainActivity.this, "没有安装" + ConstantUtil.PACKAGE_NAME, Toast.LENGTH_LONG).show();
}
public class PackageUtil {
public static Boolean checkPackInfo(Context paramContext, String paramString ) {
PackageInfo info = null;
try {
info = paramContext.getPackageManager().getPackageInfo(paramString, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (info != null) {
return true;
}
return false;
}
}
public class ConstantUtil {
public static final String PACKAGE_NAME = "com.qht.a";
}
结束进程
SuUtil.kill(ConstantUtil.PACKAGE_NAME);
public class SuUtil {
private static Process process;
public static void kill(String packageName) {
initProcess();
killProcess(packageName);
close();
}
private static void initProcess() {
if (process == null)
try {
process = Runtime.getRuntime().exec("su");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void killProcess(String packageName) {
OutputStream out = process.getOutputStream();
String cmd = "am force-stop " + packageName + " \n";
try {
out.write(cmd.getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void close() {
if (process != null)
try {
process.getOutputStream().close();
process = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
UDP收发
private DatagramSocket socket = null;
private int receivePort=1667;
private int sendPort =0;
InetAddress serverSendAddress = null;
try {
System.out.println("监听端口1667");
socket = new DatagramSocket(receivePort);
socket.setSoTimeout(5000);
} catch (Exception e) {
e.printStackTrace();
}
while (true) {
byte data[] = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet);
serverSendAddress=packet.getAddress();
sendPort=packet.getPort();
System.out.println("serverSendAddress:"+serverSendAddress.toString()+"port:"+sendPort);
} catch (SocketTimeoutException e) {
System.out.println("socket 10s 超时:" + e.getMessage());
} catch (SocketException e) {
System.out.println("socket SocketException:" + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println("socket IOException:" + e.getMessage());
e.printStackTrace();
}
String result = new String(packet.getData(), packet.getOffset(), packet.getLength());
}
String sendData ="restartSuccess";
byte data1[] = sendData.getBytes();
DatagramPacket packetSend = new DatagramPacket(data1, data1.length, serverSendAddress, sendPort);
try {
socket.send(packetSend);
System.out.println("socket.send:" + sendData);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
|