|
@@ -1,35 +1,13 @@
|
|
|
package com.usai.redant.raimage;
|
|
package com.usai.redant.raimage;
|
|
|
|
|
|
|
|
-import static android.app.PendingIntent.FLAG_IMMUTABLE;
|
|
|
|
|
-
|
|
|
|
|
-import android.app.AlarmManager;
|
|
|
|
|
-import android.app.Notification;
|
|
|
|
|
-import android.app.NotificationChannel;
|
|
|
|
|
-import android.app.NotificationManager;
|
|
|
|
|
-import android.app.PendingIntent;
|
|
|
|
|
-import android.content.BroadcastReceiver;
|
|
|
|
|
-import android.content.ComponentName;
|
|
|
|
|
-import android.content.Context;
|
|
|
|
|
-import android.content.Intent;
|
|
|
|
|
-import android.content.IntentFilter;
|
|
|
|
|
-import android.content.ServiceConnection;
|
|
|
|
|
|
|
+import android.app.Application;
|
|
|
import android.content.SharedPreferences;
|
|
import android.content.SharedPreferences;
|
|
|
-import android.os.Build;
|
|
|
|
|
-import android.os.Handler;
|
|
|
|
|
-import android.os.IBinder;
|
|
|
|
|
-import android.os.Looper;
|
|
|
|
|
-import android.os.SystemClock;
|
|
|
|
|
import android.provider.Settings;
|
|
import android.provider.Settings;
|
|
|
import android.text.TextUtils;
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
-import androidx.core.app.NotificationCompat;
|
|
|
|
|
-
|
|
|
|
|
-import com.usai.redant.rautils.application.RedantApplication;
|
|
|
|
|
-import com.usai.redant.rautils.upload.TaskActivity;
|
|
|
|
|
-import com.usai.redant.rautils.utils.RACrashHandler;
|
|
|
|
|
-import com.usai.redant.rautils.utils.RACrashHandler26;
|
|
|
|
|
-import com.usai.service.RAImageBackgroundService3;
|
|
|
|
|
|
|
+import com.usai.redant.rautils.newupload.RAUploadWorker;
|
|
|
|
|
+import com.usai.redant.rautils.utils.RACrashHandler28;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
@@ -37,8 +15,15 @@ import java.util.concurrent.locks.Lock;
|
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
|
|
|
|
|
|
-public class RAImageApplication extends RedantApplication
|
|
|
|
|
|
|
+public class RAImageApplication extends Application
|
|
|
{
|
|
{
|
|
|
|
|
+ private static RAImageApplication instance;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public static RAImageApplication getInstance() {
|
|
|
|
|
+ return instance;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
public static String CHANNEL_ID = "RAImage";
|
|
public static String CHANNEL_ID = "RAImage";
|
|
|
public static String CHANNEL_NAME = "RAImage";
|
|
public static String CHANNEL_NAME = "RAImage";
|
|
@@ -55,166 +40,169 @@ public class RAImageApplication extends RedantApplication
|
|
|
public static String device_id;
|
|
public static String device_id;
|
|
|
public static JSONObject server_info;
|
|
public static JSONObject server_info;
|
|
|
|
|
|
|
|
- private ServiceConnection mServiceConnection;
|
|
|
|
|
- private RAImageBackgroundService3 mService;
|
|
|
|
|
|
|
+// private ServiceConnection mServiceConnection;
|
|
|
|
|
+// private RAImageBackgroundService3 mService;
|
|
|
|
|
|
|
|
// public static RedAntApplication getInstance() {
|
|
// public static RedAntApplication getInstance() {
|
|
|
// return instance;
|
|
// return instance;
|
|
|
//
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
- private final BroadcastReceiver ApplicaitonReceiver = new BroadcastReceiver()
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
- public void onReceive(
|
|
|
|
|
- Context context,
|
|
|
|
|
- Intent intent)
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- int count = intent.getIntExtra("count",-1);
|
|
|
|
|
-
|
|
|
|
|
- String action = intent
|
|
|
|
|
- .getAction();
|
|
|
|
|
- if ("REDANT.RAImage.UPLOAD_UPDATE_COUNT".equals(action))
|
|
|
|
|
-
|
|
|
|
|
- {
|
|
|
|
|
- // [修改点 1] ------------ 修复启动时序问题 ------------
|
|
|
|
|
- // 如果 mService 还没绑定好(为空),则延迟 1 秒后重新执行此方法
|
|
|
|
|
- if (mService == null) {
|
|
|
|
|
- Log.w("RAImage", "mService is null in onReceive, retrying in 1s...");
|
|
|
|
|
- new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
|
|
|
|
- onReceive(context, intent); // 重新投递当前的任务
|
|
|
|
|
- }, 1000);
|
|
|
|
|
- return; // 暂停本次执行,等待重试
|
|
|
|
|
- }
|
|
|
|
|
- // [结束] ---------------------------------------
|
|
|
|
|
-
|
|
|
|
|
- TaskActivity t;
|
|
|
|
|
- PendingIntent contentIntent=PendingIntent.getActivity(getApplicationContext(),
|
|
|
|
|
- 0,new Intent(getApplicationContext(), TaskActivity.class),FLAG_IMMUTABLE);
|
|
|
|
|
- if (count>0)
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-// new Intent(getApplicationContext(), TaskActivity.class);
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-// new Intent (getApplicationContext(),TaskActivity.class);
|
|
|
|
|
- NotificationChannel channel = new NotificationChannel("Service Start",
|
|
|
|
|
- "Service Start", NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
|
- ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
|
|
|
|
|
-
|
|
|
|
|
- // [修改点 2] 将 mService 改为 context,防止 Builder 初始化崩溃
|
|
|
|
|
- Notification notification = new NotificationCompat.Builder(context, "Service Start")
|
|
|
|
|
-// .setContentTitle("Apex Land is running.")
|
|
|
|
|
- .setContentText(count + " files uploading...")
|
|
|
|
|
- .setContentIntent(contentIntent)
|
|
|
|
|
- .setCategory(Notification.CATEGORY_SERVICE).setSmallIcon(com.usai.redant.rautils.R.drawable.ic_launcher_foreground).setPriority(1000).build();
|
|
|
|
|
-
|
|
|
|
|
- // [修改点 3] 增加 try-catch 防止 Service 状态异常导致的闪退
|
|
|
|
|
- try {
|
|
|
|
|
- mService.startForeground(101, notification);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- Log.e("RAImage", "Failed to startForeground in onReceive", e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- if(count==0)
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- NotificationChannel channel = new NotificationChannel("Service Start",
|
|
|
|
|
- "Service Start", NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
|
- ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
|
|
|
|
|
-
|
|
|
|
|
- // [修改点 2] 将 mService 改为 context
|
|
|
|
|
- Notification notification = new NotificationCompat.Builder(context, "Service Start")
|
|
|
|
|
-// .setContentTitle("Apex Land is running.")
|
|
|
|
|
- .setContentText("Upload complete.")
|
|
|
|
|
- .setContentIntent(contentIntent)
|
|
|
|
|
-
|
|
|
|
|
- .setCategory(Notification.CATEGORY_SERVICE).setSmallIcon(com.usai.redant.rautils.R.drawable.ic_launcher_foreground).setPriority(1000).build();
|
|
|
|
|
-
|
|
|
|
|
- // [修改点 3] 增加 try-catch 防止 Service 状态异常导致的闪退
|
|
|
|
|
- try {
|
|
|
|
|
- mService.startForeground(101, notification);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- Log.e("RAImage", "Failed to startForeground in onReceive", e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+// private final BroadcastReceiver ApplicaitonReceiver = new BroadcastReceiver()
|
|
|
|
|
+// {
|
|
|
//
|
|
//
|
|
|
- Log.e("Finish_msg", "Application onReceive: ");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static void startalarm()
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
- Intent iAlarm = new Intent("REDANT.POP.RETRY_UPLOAD");
|
|
|
|
|
- //iAlarm.putExtra("caller", caller);
|
|
|
|
|
-// iAlarm.setAction("com.usai.apex.push");
|
|
|
|
|
- PendingIntent sender = PendingIntent.getBroadcast(getInstance(), 0,
|
|
|
|
|
- iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
-
|
|
|
|
|
- long firstime = SystemClock.elapsedRealtime();
|
|
|
|
|
-// UpdateLastAlermTime();
|
|
|
|
|
- AlarmManager am = (AlarmManager) getInstance().getSystemService(
|
|
|
|
|
- Context.ALARM_SERVICE);
|
|
|
|
|
-
|
|
|
|
|
- // 5分钟一个周期,不停的发送广播
|
|
|
|
|
- am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,
|
|
|
|
|
- 300 * 1000, sender);
|
|
|
|
|
- Log.d("redant pop","start alarm");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static void cancelalarm()
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
- // 启动完成
|
|
|
|
|
- Intent iAlarm = new Intent("REDANT.POP.RETRY_UPLOAD");
|
|
|
|
|
-// iAlarm.setAction("com.usai.apex.push");
|
|
|
|
|
- PendingIntent sender = PendingIntent.getBroadcast(getInstance(), 0,
|
|
|
|
|
- iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
+// public void onReceive(
|
|
|
|
|
+// Context context,
|
|
|
|
|
+// Intent intent)
|
|
|
|
|
+// {
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+// int count = intent.getIntExtra("count",-1);
|
|
|
|
|
+//
|
|
|
|
|
+// String action = intent
|
|
|
|
|
+// .getAction();
|
|
|
|
|
+// if ("REDANT.RAImage.UPLOAD_UPDATE_COUNT".equals(action))
|
|
|
|
|
+//
|
|
|
|
|
+// {
|
|
|
|
|
+// // [修改点 1] ------------ 修复启动时序问题 ------------
|
|
|
|
|
+// // 如果 mService 还没绑定好(为空),则延迟 1 秒后重新执行此方法
|
|
|
|
|
+//// if (mService == null) {
|
|
|
|
|
+//// Log.w("RAImage", "mService is null in onReceive, retrying in 1s...");
|
|
|
|
|
+//// new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
|
|
|
|
+//// onReceive(context, intent); // 重新投递当前的任务
|
|
|
|
|
+//// }, 1000);
|
|
|
|
|
+//// return; // 暂停本次执行,等待重试
|
|
|
|
|
+//// }
|
|
|
|
|
+// // [结束] ---------------------------------------
|
|
|
|
|
+//
|
|
|
|
|
+// NewTaskActivity t;
|
|
|
|
|
+// PendingIntent contentIntent=PendingIntent.getActivity(getApplicationContext(),
|
|
|
|
|
+// 0,new Intent(getApplicationContext(), NewTaskActivity.class),FLAG_IMMUTABLE);
|
|
|
|
|
+//// if (count>0)
|
|
|
|
|
+//// {
|
|
|
|
|
+////
|
|
|
|
|
+////
|
|
|
|
|
+////// new Intent(getApplicationContext(), TaskActivity.class);
|
|
|
|
|
+////
|
|
|
|
|
+////
|
|
|
|
|
+////// new Intent (getApplicationContext(),TaskActivity.class);
|
|
|
|
|
+//// NotificationChannel channel = new NotificationChannel("Service Start",
|
|
|
|
|
+//// "Service Start", NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
|
+//// ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
|
|
|
|
|
+////
|
|
|
|
|
+//// // [修改点 2] 将 mService 改为 context,防止 Builder 初始化崩溃
|
|
|
|
|
+//// Notification notification = new NotificationCompat.Builder(context, "Service Start")
|
|
|
|
|
+////// .setContentTitle("Apex Land is running.")
|
|
|
|
|
+//// .setContentText(count + " files uploading...")
|
|
|
|
|
+//// .setContentIntent(contentIntent)
|
|
|
|
|
+//// .setCategory(Notification.CATEGORY_SERVICE).setSmallIcon(com.usai.redant.rautils.R.drawable.ic_launcher_foreground).setPriority(1000).build();
|
|
|
|
|
+////
|
|
|
|
|
+//// // [修改点 3] 增加 try-catch 防止 Service 状态异常导致的闪退
|
|
|
|
|
+//// try {
|
|
|
|
|
+//// mService.startForeground(101, notification);
|
|
|
|
|
+//// } catch (Exception e) {
|
|
|
|
|
+//// Log.e("RAImage", "Failed to startForeground in onReceive", e);
|
|
|
|
|
+//// }
|
|
|
|
|
+//// }
|
|
|
|
|
+//// else
|
|
|
|
|
+//// if(count==0)
|
|
|
|
|
+//// {
|
|
|
|
|
+////
|
|
|
|
|
+////
|
|
|
|
|
+////
|
|
|
|
|
+//// NotificationChannel channel = new NotificationChannel("Service Start",
|
|
|
|
|
+//// "Service Start", NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
|
+//// ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
|
|
|
|
|
+////
|
|
|
|
|
+//// // [修改点 2] 将 mService 改为 context
|
|
|
|
|
+//// Notification notification = new NotificationCompat.Builder(context, "Service Start")
|
|
|
|
|
+////// .setContentTitle("Apex Land is running.")
|
|
|
|
|
+//// .setContentText("Upload complete.")
|
|
|
|
|
+//// .setContentIntent(contentIntent)
|
|
|
|
|
+////
|
|
|
|
|
+//// .setCategory(Notification.CATEGORY_SERVICE).setSmallIcon(com.usai.redant.rautils.R.drawable.ic_launcher_foreground).setPriority(1000).build();
|
|
|
|
|
+////
|
|
|
|
|
+//// // [修改点 3] 增加 try-catch 防止 Service 状态异常导致的闪退
|
|
|
|
|
+//// try {
|
|
|
|
|
+//// mService.startForeground(101, notification);
|
|
|
|
|
+//// } catch (Exception e) {
|
|
|
|
|
+//// Log.e("RAImage", "Failed to startForeground in onReceive", e);
|
|
|
|
|
+//// }
|
|
|
|
|
+//// }
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+////
|
|
|
|
|
+// Log.e("Finish_msg", "Application onReceive: ");
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// };
|
|
|
|
|
|
|
|
- AlarmManager am = (AlarmManager) getInstance().getSystemService(
|
|
|
|
|
- Context.ALARM_SERVICE);
|
|
|
|
|
|
|
|
|
|
- am.cancel(sender);
|
|
|
|
|
- Log.d("redant pop","cancel alarm");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+//
|
|
|
|
|
+// public static void startalarm()
|
|
|
|
|
+// {
|
|
|
|
|
+//
|
|
|
|
|
+// Intent iAlarm = new Intent("REDANT.POP.RETRY_UPLOAD");
|
|
|
|
|
+// //iAlarm.putExtra("caller", caller);
|
|
|
|
|
+//// iAlarm.setAction("com.usai.apex.push");
|
|
|
|
|
+// PendingIntent sender = PendingIntent.getBroadcast(getInstance(), 0,
|
|
|
|
|
+// iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
+//
|
|
|
|
|
+// long firstime = SystemClock.elapsedRealtime();
|
|
|
|
|
+//// UpdateLastAlermTime();
|
|
|
|
|
+// AlarmManager am = (AlarmManager) getInstance().getSystemService(
|
|
|
|
|
+// Context.ALARM_SERVICE);
|
|
|
|
|
+//
|
|
|
|
|
+// // 5分钟一个周期,不停的发送广播
|
|
|
|
|
+// am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,
|
|
|
|
|
+// 300 * 1000, sender);
|
|
|
|
|
+// Log.d("redant pop","start alarm");
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// public static void cancelalarm()
|
|
|
|
|
+// {
|
|
|
|
|
+//
|
|
|
|
|
+// // 启动完成
|
|
|
|
|
+// Intent iAlarm = new Intent("REDANT.POP.RETRY_UPLOAD");
|
|
|
|
|
+//// iAlarm.setAction("com.usai.apex.push");
|
|
|
|
|
+// PendingIntent sender = PendingIntent.getBroadcast(getInstance(), 0,
|
|
|
|
|
+// iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
+//
|
|
|
|
|
+// AlarmManager am = (AlarmManager) getInstance().getSystemService(
|
|
|
|
|
+// Context.ALARM_SERVICE);
|
|
|
|
|
+//
|
|
|
|
|
+// am.cancel(sender);
|
|
|
|
|
+// Log.d("redant pop","cancel alarm");
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void onCreate() {
|
|
public void onCreate() {
|
|
|
|
|
|
|
|
- RACrashHandler26.init(this);
|
|
|
|
|
|
|
+ RACrashHandler28.init(this);
|
|
|
|
|
+
|
|
|
// TODO Auto-generated method stub
|
|
// TODO Auto-generated method stub
|
|
|
Log.d("_RAIMAGE", "onCreate: RedAntApplication");
|
|
Log.d("_RAIMAGE", "onCreate: RedAntApplication");
|
|
|
super.onCreate();
|
|
super.onCreate();
|
|
|
|
|
|
|
|
- IntentFilter msgFilter = new IntentFilter();
|
|
|
|
|
- msgFilter.addAction("REDANT.RAImage.UPLOAD_UPDATE_COUNT");
|
|
|
|
|
-
|
|
|
|
|
- msgFilter.setPriority(2147483647);
|
|
|
|
|
-
|
|
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
|
|
|
|
|
- {
|
|
|
|
|
- registerReceiver(ApplicaitonReceiver, msgFilter,RECEIVER_NOT_EXPORTED);
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
|
|
+ instance = this;
|
|
|
|
|
|
|
|
- registerReceiver(ApplicaitonReceiver, msgFilter);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// IntentFilter msgFilter = new IntentFilter();
|
|
|
|
|
+// msgFilter.addAction("REDANT.RAImage.UPLOAD_UPDATE_COUNT");
|
|
|
|
|
+//
|
|
|
|
|
+// msgFilter.setPriority(2147483647);
|
|
|
|
|
+//
|
|
|
|
|
+// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
|
|
|
|
|
+// {
|
|
|
|
|
+// registerReceiver(ApplicaitonReceiver, msgFilter,RECEIVER_NOT_EXPORTED);
|
|
|
|
|
+// }
|
|
|
|
|
+// else
|
|
|
|
|
+// {
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+// registerReceiver(ApplicaitonReceiver, msgFilter);
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
|
|
|
|
|
// setInstance(this);
|
|
// setInstance(this);
|
|
@@ -234,84 +222,89 @@ public class RAImageApplication extends RedantApplication
|
|
|
station_name = name;
|
|
station_name = name;
|
|
|
|
|
|
|
|
/** Service */
|
|
/** Service */
|
|
|
- mServiceConnection = new ServiceConnection() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onServiceConnected(ComponentName name, IBinder service) {
|
|
|
|
|
-
|
|
|
|
|
- RAImageBackgroundService3.Service3Binder binder = (RAImageBackgroundService3.Service3Binder)service;
|
|
|
|
|
- mService = (RAImageBackgroundService3)binder.getService();
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onServiceDisconnected(ComponentName name) {
|
|
|
|
|
-
|
|
|
|
|
- mService = null;
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- Intent serviceIntent = new Intent();
|
|
|
|
|
- serviceIntent.setClass(this, RAImageBackgroundService3.class);
|
|
|
|
|
-
|
|
|
|
|
- // [修改点 4] ------------ 修复 ForegroundServiceDidNotStopInTimeException ------------
|
|
|
|
|
- // 策略:尽量使用普通的 startService,只有在后台被系统拦截时(IllegalStateException),
|
|
|
|
|
- // 才不得已使用 startForegroundService。这能避免系统因 Service 启动慢而直接杀死 App。
|
|
|
|
|
- try {
|
|
|
|
|
- // 1. 优先尝试普通启动(安全,不会崩)
|
|
|
|
|
- this.startService(serviceIntent);
|
|
|
|
|
- } catch (IllegalStateException e) {
|
|
|
|
|
- // 2. 捕获到 IllegalStateException 说明 App 在后台,必须用 Foreground Service
|
|
|
|
|
- try {
|
|
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
- this.startForegroundService(serviceIntent);
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception innerE) {
|
|
|
|
|
- // 如果还是起不来(比如 Android 14 后台限制),记录日志,但不要让 App 闪退
|
|
|
|
|
- Log.e("RAImage", "Failed to startForegroundService fallback", innerE);
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- // [修改结束] -------------------------------------------------------------------
|
|
|
|
|
-
|
|
|
|
|
- Intent intent = new Intent(getApplicationContext(),RAImageBackgroundService3.class);
|
|
|
|
|
- bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);
|
|
|
|
|
|
|
+// mServiceConnection = new ServiceConnection() {
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onServiceConnected(ComponentName name, IBinder service) {
|
|
|
|
|
+//
|
|
|
|
|
+// RAImageBackgroundService3.Service3Binder binder = (RAImageBackgroundService3.Service3Binder)service;
|
|
|
|
|
+// mService = (RAImageBackgroundService3)binder.getService();
|
|
|
|
|
+//
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onServiceDisconnected(ComponentName name) {
|
|
|
|
|
+//
|
|
|
|
|
+// mService = null;
|
|
|
|
|
+// }
|
|
|
|
|
+// };
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+// Intent serviceIntent = new Intent();
|
|
|
|
|
+// serviceIntent.setClass(this, RAImageBackgroundService3.class);
|
|
|
|
|
+//
|
|
|
|
|
+// // [修改点 4] ------------ 修复 ForegroundServiceDidNotStopInTimeException ------------
|
|
|
|
|
+// // 策略:尽量使用普通的 startService,只有在后台被系统拦截时(IllegalStateException),
|
|
|
|
|
+// // 才不得已使用 startForegroundService。这能避免系统因 Service 启动慢而直接杀死 App。
|
|
|
|
|
+// try {
|
|
|
|
|
+// // 1. 优先尝试普通启动(安全,不会崩)
|
|
|
|
|
+// this.startService(serviceIntent);
|
|
|
|
|
+// } catch (IllegalStateException e) {
|
|
|
|
|
+// // 2. 捕获到 IllegalStateException 说明 App 在后台,必须用 Foreground Service
|
|
|
|
|
+// try {
|
|
|
|
|
+// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
+// this.startForegroundService(serviceIntent);
|
|
|
|
|
+// }
|
|
|
|
|
+// } catch (Exception innerE) {
|
|
|
|
|
+// // 如果还是起不来(比如 Android 14 后台限制),记录日志,但不要让 App 闪退
|
|
|
|
|
+// Log.e("RAImage", "Failed to startForegroundService fallback", innerE);
|
|
|
|
|
+// }
|
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
|
+// e.printStackTrace();
|
|
|
|
|
+// }
|
|
|
|
|
+// // [修改结束] -------------------------------------------------------------------
|
|
|
|
|
+//
|
|
|
|
|
+// Intent intent = new Intent(getApplicationContext(),RAImageBackgroundService3.class);
|
|
|
|
|
+// bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);
|
|
|
|
|
|
|
|
/***/
|
|
/***/
|
|
|
|
|
|
|
|
- RACrashHandler.init(this);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public Class getServiceClass() {
|
|
|
|
|
- return mService.getClass();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public boolean useFakeData()
|
|
|
|
|
- {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+// RACrashHandler.init(this);
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onTerminate() {
|
|
|
|
|
- Log.d("_RAIMAGE", "onTerminate: RedAntApplication");
|
|
|
|
|
- super.onTerminate();
|
|
|
|
|
|
|
|
|
|
- unbindService(mServiceConnection);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- unregisterReceiver(ApplicaitonReceiver);
|
|
|
|
|
|
|
+ RAUploadWorker.schedulePeriodic(this);
|
|
|
}
|
|
}
|
|
|
|
|
+//
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public Class getServiceClass() {
|
|
|
|
|
+// return mService.getClass();
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public boolean useFakeData()
|
|
|
|
|
+// {
|
|
|
|
|
+// return false;
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- protected void finalize() throws Throwable {
|
|
|
|
|
|
|
|
|
|
- Log.d("_RAIMAGE", "finalize: RedAntApplication");
|
|
|
|
|
- super.finalize();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onTerminate() {
|
|
|
|
|
+// Log.d("_RAIMAGE", "onTerminate: RedAntApplication");
|
|
|
|
|
+// super.onTerminate();
|
|
|
|
|
+//
|
|
|
|
|
+//// unbindService(mServiceConnection);
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+//// unregisterReceiver(ApplicaitonReceiver);
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// @Override
|
|
|
|
|
+// protected void finalize() throws Throwable {
|
|
|
|
|
+//
|
|
|
|
|
+// Log.d("_RAIMAGE", "finalize: RedAntApplication");
|
|
|
|
|
+// super.finalize();
|
|
|
|
|
+// }
|
|
|
}
|
|
}
|