2 Commits 843a811354 ... d2f58c36ce

Autore SHA1 Messaggio Data
  guilin d2f58c36ce Merge branch 'master' into master_925 1 mese fa
  guilin c27a810446 自动上传优化 1 mese fa

+ 0 - 28
app/src/main/java/com/fxy/base/ChargeStateReceiver.java

@@ -1,28 +0,0 @@
-package com.fxy.base;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.os.BatteryManager;
-
-import com.fxy.utils.JobUtils;
-
-public class ChargeStateReceiver extends BroadcastReceiver {
-
-    @Override
-    public void onReceive(Context context, Intent intent) {
-        if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
-            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
-            boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
-                    status == BatteryManager.BATTERY_STATUS_FULL;
-
-            // 处理充电状态的变化
-            if (isCharging) {
-                // 设备正在充电
-                JobUtils.chargingJob(context);
-            } else {
-                // 设备不在充电
-            }
-        }
-    }
-}

+ 67 - 0
app/src/main/java/com/fxy/base/DeviceStatusReceiver.java

@@ -0,0 +1,67 @@
+package com.fxy.base;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.BatteryManager;
+import android.util.Log;
+
+import com.fxy.utils.JobUtils;
+
+public class DeviceStatusReceiver extends BroadcastReceiver {
+
+    private static final String TAG = "DeviceStatusReceiver";
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        String action = intent.getAction();
+        if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
+            handleBatteryChanged(context, intent);
+        } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
+            handleScreenOn(context);
+        } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
+            handleScreenOff(context);
+        }
+    }
+
+    private void handleBatteryChanged(Context context, Intent intent) {
+        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
+        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING;
+        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
+        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
+        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
+
+        Log.d(TAG, "Charging state: " + isCharging);
+        Log.d(TAG, "USB charging: " + usbCharge);
+        Log.d(TAG, "AC charging: " + acCharge);
+
+        if (isCharging) {
+            onCharging(context);
+        } else {
+            onNotCharging(context);
+        }
+    }
+
+    private void handleScreenOn(Context context) {
+        Log.d(TAG, "Screen turned ON");
+        // 屏幕打开时的操作
+    }
+
+    private void handleScreenOff(Context context) {
+        Log.d(TAG, "屏幕关闭时");
+        // 屏幕关闭时的操作
+        // 设备正在充电
+        JobUtils.deviceIdleJob(context);
+    }
+
+    private void onCharging(Context context) {
+        // 当设备正在充电时的操作
+        Log.d(TAG, "Device is charging!");
+        // 设备正在充电
+        JobUtils.chargingJob(context);
+    }
+
+    private void onNotCharging(Context context) {
+        // 当设备不在充电时的操作
+        Log.d(TAG, "Device is not charging!");
+    }
+}

+ 2 - 1
app/src/main/java/com/fxy/common/OssService.java

@@ -225,7 +225,7 @@ public class OssService {
             taskId = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
                 @Override
                 public void onSuccess(PutObjectRequest request, PutObjectResult result) {
-
+                    UploadUtils.delUploadTime(mContext);
                     if (BuildConfig.DEBUG){
                         XLog.e(request);
                     }
@@ -242,6 +242,7 @@ public class OssService {
 
                 @Override
                 public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
+                    UploadUtils.delUploadTime(mContext);
                     Message msg = new Message();
                     msg.what = 1;
                     msg.arg1 = current;

+ 3 - 3
app/src/main/java/com/fxy/main/MainActivity.java

@@ -8,7 +8,7 @@ import android.support.v4.content.ContextCompat;
 import android.support.v4.view.ViewPager;
 import android.view.KeyEvent;
 
-import com.fxy.base.ChargeStateReceiver;
+import com.fxy.base.DeviceStatusReceiver;
 import com.jaeger.library.StatusBarUtil;
 import com.jpeng.jptabbar.BadgeDismissListener;
 import com.jpeng.jptabbar.JPTabBar;
@@ -104,7 +104,7 @@ public class MainActivity extends BaseActivity implements BadgeDismissListener,
     }
 
 
-    private ChargeStateReceiver receiver;
+    private DeviceStatusReceiver receiver;
     private IntentFilter filter;
     @Override
     protected void initView() {
@@ -119,7 +119,7 @@ public class MainActivity extends BaseActivity implements BadgeDismissListener,
 
 
         // 创建 BroadcastReceiver
-        receiver = new ChargeStateReceiver();
+        receiver = new DeviceStatusReceiver();
         // 创建 IntentFilter
         filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
         // 注册 BroadcastReceiver

+ 2 - 4
app/src/main/java/com/fxy/service/UploadJobService.java

@@ -6,7 +6,6 @@ import android.content.Context;
 
 import com.elvishew.xlog.XLog;
 import com.fxy.base.UploadContentProvider;
-import com.fxy.baselibrary.language.other.CommSharedUtil;
 import com.fxy.common.AsyncFileUpload;
 
 public class UploadJobService  extends JobService {
@@ -22,13 +21,11 @@ public class UploadJobService  extends JobService {
         // 2. 上传文件到OSS
 
         Context context = UploadContentProvider.getCurrentContext();
-
         XLog.d("------------执行异步上传图片--------------------"+jobParameters.getJobId());
-        asyncFileUpload = new AsyncFileUpload(context);
 
 
+        asyncFileUpload = new AsyncFileUpload(context);
         Integer[] pushStatus = {0,1};
-
         int awaitNum = asyncFileUpload.getAllAwaitNum(pushStatus);
         if (awaitNum == 0){
             // 标记任务为已完成(如果你不需要重试)
@@ -37,6 +34,7 @@ public class UploadJobService  extends JobService {
         }else{
             asyncFileUpload.setStartUpload();
             jobFinished(jobParameters, true);
+            // 返回 true 表示任务正在执行中
             return true;
         }
 

+ 2 - 1
app/src/main/java/com/fxy/utils/JobUtils.java

@@ -49,7 +49,8 @@ public class JobUtils {
         builder.setMinimumLatency(1000 * 60 * leisure); // 延迟至少60秒
 //        builder.setMinimumLatency(10); // 延迟至少60秒
         builder.setOverrideDeadline(4*60 * 1000 * 60); // 最多延迟2小时
-
+//        builder.setPersisted(true); // 任务会被持久化
+//        builder.setPeriodic(15 * 60 * 1000); // 设置周期性任务的时间间隔
         JobInfo jobInfo = builder.build();
 
 

+ 6 - 7
app/src/main/java/com/fxy/utils/UploadUtils.java

@@ -32,6 +32,7 @@ public class UploadUtils {
     }
     public static long getCurrentTime(){
         Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.MILLISECOND, 0);
         return calendar.getTimeInMillis();
     }
 
@@ -51,13 +52,13 @@ public class UploadUtils {
     }
 
     public static boolean getUploadExists(Context context){
-       String  currentUploadTimeStr = CommSharedUtil.getInstance(context).getString(currentUploadTimeTag,"");
+       String  currentUploadTimeStr = CommSharedUtil.getInstance(context).getString(currentUploadTimeTag);
 
-       if (currentUploadTimeStr.isEmpty()){
+       if (currentUploadTimeStr==null ||currentUploadTimeStr.isEmpty()){
            return true;
        }
-        long diff = calculateDifferenceInSeconds( getCurrentTime() , Long.getLong(currentUploadTimeStr));
-        return diff >= 60 * 5;
+        long diff = calculateDifferenceInSeconds( getCurrentTime() , Long.parseLong(currentUploadTimeStr));
+        return diff <= 60 * 2;
     }
 
     /**
@@ -71,11 +72,9 @@ public class UploadUtils {
     /**
      * 删除上传时间
      * @param context Context
-     * @return boolean
      */
-    public static boolean delUploadTime(Context context){
+    public static void delUploadTime(Context context){
         CommSharedUtil.getInstance(context).remove(currentUploadTimeTag);
-        return true;
     }
 
 }