Android 實現釘釘自動打卡功能

提前準備

首先我們需要一直不用的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執行定時任務

  • cron4j.jar下載地址
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();

    }

  }

以上就是Android 實現釘釘自動打卡功能的詳細內容,更多關於Android 釘釘自動打卡的資料請關註WalkonNet其它相關文章!

推薦閱讀: