Преглед изворни кода

提交自动时长任务时自动累加数据库时间与当前时间

zengjiebin пре 8 година
родитељ
комит
f601452119

+ 19 - 0
app/src/main/java/com/sheep/jiuyan/samllsheep/SheepApp.java

@@ -10,6 +10,7 @@ import com.kfzs.duanduan.react.SharedPreferences;
 import com.sheep.gamegroup.di.components.DaggerNetComponent;
 import com.sheep.gamegroup.di.components.NetComponent;
 import com.sheep.gamegroup.di.modules.NetModule;
+import com.sheep.gamegroup.model.entity.AppRecord;
 import com.sheep.gamegroup.model.entity.TaskEty;
 import com.sheep.gamegroup.util.ConnectAddress;
 import com.sheep.gamegroup.util.Jump2View;
@@ -30,6 +31,24 @@ public class SheepApp extends BaseApplication {
 
     private NetComponent netComponent;
     private TaskEty taskEty;
+    private AppRecord lastAppRecord;
+    private long lastTime;
+
+    public AppRecord getLastAppRecord() {
+        return lastAppRecord;
+    }
+
+    public long getLastTime() {
+        return lastTime;
+    }
+
+    public void setLastAppRecord(AppRecord lastAppRecord) {
+        this.lastAppRecord = lastAppRecord;
+    }
+
+    public void setLastTime(long lastTime) {
+        this.lastTime = lastTime;
+    }
 
     public TaskEty getTaskEty() {
         return taskEty;

+ 7 - 0
app/src/main/java/com/sheep/jiuyan/samllsheep/service/FloatService.java

@@ -20,6 +20,7 @@ import android.widget.LinearLayout;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.sheep.gamegroup.model.entity.AppRecord;
 import com.sheep.gamegroup.model.entity.BaseMessage;
 import com.sheep.gamegroup.model.entity.TaskEty;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
@@ -119,6 +120,12 @@ public class FloatService extends Service implements View.OnClickListener {
                     JSONObject object = new JSONObject();
                     object.put("gps", gps);
                     long runTime = MyDbManager.getInstance().getAppRunTime(taskEty.getPackage_names());
+
+                    AppRecord lastAppRecord = SheepApp.getInstance().getLastAppRecord();
+                    if(lastAppRecord != null && TextUtils.equals(taskEty.getPackage_names(), lastAppRecord.getPackageName())){
+                        runTime +=  SheepApp.getInstance().getLastTime() - System.currentTimeMillis();
+                    }
+
                     MyDbManager.getInstance().removeAppRecord();
                     System.out.println("MonitorAppService runTime = "+runTime);
                     object.put("task_time", runTime/1000);

+ 6 - 4
app/src/main/java/com/sheep/jiuyan/samllsheep/service/MonitorAppService.java

@@ -11,6 +11,7 @@ import com.sheep.gamegroup.model.entity.AppRecord;
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.ListUtil;
 import com.sheep.gamegroup.util.MyDbManager;
+import com.sheep.jiuyan.samllsheep.SheepApp;
 
 import java.util.List;
 
@@ -19,8 +20,6 @@ import java.util.List;
  * realicing@sina.com
  */
 public class MonitorAppService extends AccessibilityService {
-    private AppRecord lastAppRecord;
-    private long lastTime;
     @Override
     public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
         // 此方法是在主线程中回调过来的,所以消息是阻塞执行的
@@ -40,13 +39,16 @@ public class MonitorAppService extends AccessibilityService {
                 appRecord.setPackageName(packageName.toString());
                 if(!textList.isEmpty())
                     appRecord.setTag(textList.get(0).toString());
+
+                AppRecord lastAppRecord = SheepApp.getInstance().getLastAppRecord();
+                long lastTime = SheepApp.getInstance().getLastTime();
                 if(lastAppRecord != null && lastTime != 0)
                     lastAppRecord.setTime(lastAppRecord.getTime() + curTime - lastTime);
                 if(lastAppRecord != null)
                     MyDbManager.getInstance().saveOrUpdate(lastAppRecord);
 
-                lastTime = curTime;
-                lastAppRecord = appRecord;
+                SheepApp.getInstance().setLastTime(curTime);
+                SheepApp.getInstance().setLastAppRecord(appRecord);
                 break;
 //            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
 //                System.out.println("MonitorAppService TYPE_WINDOW_CONTENT_CHANGED: " + " packageName: " + packageName + " source: " + ListUtil.getIndex(textList, 0));