提前准备
首先我们需要一直不用的Android手机,插上公司的电源。下载安装钉钉并设置为极速打卡。
自动打开
我们只需要启动一个服务,定时打开钉钉应用后再返回即可。
防止熄屏
这华为手机并没有永不息屏的选项,所以我们需要设置一下防止自动息屏。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置不自动息屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
}
打开钉钉,签到后返回
核心程序
/**
*
* @param packageName 包名
*/
public void openApplication(String packageName) {
PackageManager packageManager = getPackageManager();
PackageInfo pi = null;
try {
pi = packageManager.getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
}
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List<ResolveInfo> apps = packageManager.queryIntentActivities(resolveIntent, 0);
ResolveInfo resolveInfo = apps.iterator().next();
if (resolveInfo != null) {
String className = resolveInfo.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
}
}
- 打开cmd查找钉钉的包名
adb devices
adb shell
pm list package | grep alibaba 查找阿里巴巴相关的所有包名,也可以直接pm list package
查找所有包名
// 打开钉钉
String packageName = "com.alibaba.android.rimet";
openApplication(packageName);
// 延迟2s,自动打开
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 返回自动打卡app
String packageNameNew = "com.example.dingding";
openApplication(packageNameNew);
创建一个service来进行自动打卡,使用java的cron4j库来执行定时任务,使用可以参见以前的博客。JFinal用cron4j执行定时任务
public class DingService extends Service {
private final String TAG = "DingService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "Service Start!");
ClockThread ct = new ClockThread();
ct.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
/**
*
* @param packageName 包名
*/
public void openApplication(String packageName) {...}
public class ClockThread extends Thread {
public void run() {
Scheduler scheduler = new Scheduler();
//设置打开时间为工作日的 7:50,11:50,13:50,17:50
scheduler.schedule("50 7,11,13,17 * * mon,tue,wed,thu,fri", new Thread() {
@Override
public void run() {
String packageName = "com.alibaba.android.rimet";
openApplication(packageName);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String packageNameNew = "com.example.dingding";
openApplication(packageNameNew);
}
});
scheduler.start();
}
}
}
最后在MainActivity中启动服务即可。
随机时间
为了让打卡更加真实,每天的打卡时间尽可能尽量不一样。我们用一个Scheduler(调度任务)每天在固定时间如每天早上6点,创建四个时间随机的调度任务来完成每日的打卡。
public class ClockThread extends Thread {
public void run() {
scheduler = new Scheduler();
scheduler.schedule("0 6 * * mon,tue,wed,thu,fri", new Thread() {
@Override
public void run() {
if(scheduler1 != null){
scheduler1.stop();
scheduler1 = null;
}
if(scheduler2 != null){
scheduler2.stop();
scheduler2 = null;
}
if(scheduler3 != null){
scheduler3.stop();
scheduler3 = null;
}
if(scheduler4 != null){
scheduler4.stop();
scheduler4 = null;
}
// 随机在7:00 ~ 7:27之间打卡
// scheduler1 scheduler.schedule(new Random().nextInt(28)+" 7 * * *",new Thread())
// 随机在11:30 ~ 11:57之间打卡
// scheduler2 scheduler.schedule((30 + new Random().nextInt(28))+" 11 * * *",new Thread())
// 随机在13:00 ~ 13:27之间打卡
// scheduler3 scheduler.schedule(new Random().nextInt(28)+" 13 * * *",new Thread())
// 随机在17:30 ~ 17:57之间打卡
// scheduler4 scheduler.schedule((30 + new Random().nextInt(28))+" 17 * * *",new Thread())
}
});
scheduler.start();
}
}
来源:oschina
链接:https://my.oschina.net/u/4312590/blog/4332180