//定时器
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函数
/**
* 跳转第三方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);
/**
*
* 执行android命令
*/
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;//唤醒对方的ip
//udp接收
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对象
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()); //接受到的结果
}
//udp发送
String sendData ="restartSuccess";
byte data1[] = sendData.getBytes();
// 创建一个发送数据的DatagramPacket对象
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();
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)