magisk模組 實現app 保活

七六五發表於2024-08-01
#!/system/bin/sh
MODDIR=${0%/*}

while [ "$(getprop sys.boot_completed)" != "1" ]; do
        sleep 1
    done
    
echo "PowerManagerService.noSuspend" > /sys/power/wake_lock

targetTime="4"
targetTime2="16"
executedToday=false
appPackage="com.idormy.sms.forwarder"
appActivity="com.idormy.sms.forwarder/com.idormy.sms.forwarder.MainActivity"

log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') $1"
}

while true; do
  # 每30分鐘執行一次
  log "定時喚醒"
  am start -n "$appActivity"
  sleep 1800  # 30分鐘

  ctime=$(date +"%H")
  currentDate=$(date +"%Y-%m-%d")

  log "Current hour: $ctime"
  log "Target times: $targetTime, $targetTime2"

  if [ "$ctime" = "$targetTime" ] || [ "$ctime" = "$targetTime2" ]; then
    if [ "$executedToday" = false ]; then
      log "每天4點殺死app重新啟動"
      am force-stop "$appPackage"
      sleep 2
      am start -n "$appActivity"
      executedToday=true
      log "Executed restart at $currentDate $ctime:00"
    else
      log "Already executed restart today."
    fi
  else
    # 重置標誌變數,每天0點之後允許再次執行
    if [ "$ctime" = "00" ]; then
      executedToday=false
      log "Reset executedToday flag"
    fi
    log "It is not the target time, skipping force-stop and restart"
  fi
done

每30分鐘自動喚醒一次, 每天4點殺app重啟一次

相關文章