diff --git a/appbase/build.properties b/appbase/build.properties
index b1b84b7..4b5cf0d 100644
--- a/appbase/build.properties
+++ b/appbase/build.properties
@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
-#Wed Feb 19 03:19:10 HKT 2025
+#Wed Feb 19 11:49:56 GMT 2025
stageCount=4
libraryProject=libappbase
baseVersion=1.5
publishVersion=1.5.3
-buildCount=0
+buildCount=61
baseBetaVersion=1.5.4
diff --git a/appbase/src/main/AndroidManifest.xml b/appbase/src/main/AndroidManifest.xml
index ac115df..f508b74 100644
--- a/appbase/src/main/AndroidManifest.xml
+++ b/appbase/src/main/AndroidManifest.xml
@@ -54,9 +54,7 @@
-
-
@@ -66,8 +64,6 @@
android:exported="true">
-
-
diff --git a/appbase/src/main/java/cc/winboll/studio/appbase/MainActivity.java b/appbase/src/main/java/cc/winboll/studio/appbase/MainActivity.java
index 0fb013f..3e4cac0 100644
--- a/appbase/src/main/java/cc/winboll/studio/appbase/MainActivity.java
+++ b/appbase/src/main/java/cc/winboll/studio/appbase/MainActivity.java
@@ -95,19 +95,22 @@ public class MainActivity extends AppCompatActivity {
public void onStartTestService(View view) {
Intent intent = new Intent(this, TestService.class);
+ intent.setAction(SOS.ACTION_SERVICE_ENABLE);
startService(intent);
}
public void onStopTestService(View view) {
Intent intent = new Intent(this, TestService.class);
- stopService(intent);
-
+ intent.setAction(SOS.ACTION_SERVICE_DISABLE);
+ startService(intent);
+
+ Intent intentStop = new Intent(this, TestService.class);
+ stopService(intentStop);
}
- public void onUpdateAPPWidget(View view) {
- Intent intentAPPWidget = new Intent(this, StatusWidget.class);
- intentAPPWidget.setAction(StatusWidget.ACTION_STATUS_UPDATE);
- sendBroadcast(intentAPPWidget);
+ public void onStopTestServiceNoSettings(View view) {
+ Intent intent = new Intent(this, TestService.class);
+ stopService(intent);
}
}
diff --git a/appbase/src/main/java/cc/winboll/studio/appbase/receivers/MainReceiver.java b/appbase/src/main/java/cc/winboll/studio/appbase/receivers/MainReceiver.java
index bfb963c..a791fea 100644
--- a/appbase/src/main/java/cc/winboll/studio/appbase/receivers/MainReceiver.java
+++ b/appbase/src/main/java/cc/winboll/studio/appbase/receivers/MainReceiver.java
@@ -5,13 +5,24 @@ package cc.winboll.studio.appbase.receivers;
* @Date 2025/02/13 06:58:04
* @Describe 主要广播接收器
*/
+import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import cc.winboll.studio.appbase.beans.SOSReportBean;
import cc.winboll.studio.appbase.services.MainService;
+import cc.winboll.studio.appbase.widgets.SOSWidget;
+import cc.winboll.studio.libappbase.AppUtils;
+import cc.winboll.studio.libappbase.LogUtils;
+import cc.winboll.studio.libappbase.SOS;
+import cc.winboll.studio.libappbase.bean.APPSOSBean;
import com.hjq.toast.ToastUtils;
+import java.io.IOException;
import java.lang.ref.WeakReference;
+import java.text.SimpleDateFormat;
+import java.util.Date;
public class MainReceiver extends BroadcastReceiver {
@@ -32,6 +43,74 @@ public class MainReceiver extends BroadcastReceiver {
String szAction = intent.getAction();
if (szAction.equals(ACTION_BOOT_COMPLETED)) {
ToastUtils.show("ACTION_BOOT_COMPLETED");
+ } else if (szAction.equals(SOS.ACTION_BIND)) {
+ LogUtils.d(TAG, "ACTION_BIND");
+ LogUtils.d(TAG, String.format("context.getPackageName() %s", context.getPackageName()));
+ LogUtils.d(TAG, String.format("intent.getAction() %s", intent.getAction()));
+ String SOS = intent.getStringExtra("SOS");
+ LogUtils.d(TAG, String.format("SOS %s", SOS));
+ if (SOS != null && SOS.equals("Service")) {
+ String szAPPSOSBean = intent.getStringExtra("APPSOSBean");
+ LogUtils.d(TAG, String.format("szAPPSOSBean %s", szAPPSOSBean));
+ if (szAPPSOSBean != null && !szAPPSOSBean.equals("")) {
+ try {
+ APPSOSBean bean = APPSOSBean.parseStringToBean(szAPPSOSBean, APPSOSBean.class);
+ if (bean != null) {
+ String sosPackage = bean.getSosPackage();
+ LogUtils.d(TAG, String.format("sosPackage %s", sosPackage));
+ String sosClassName = bean.getSosClassName();
+ LogUtils.d(TAG, String.format("sosClassName %s", sosClassName));
+ mwrService.get().bindSOSConnection(bean);
+ }
+ } catch (IOException e) {
+ LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
+ }
+ }
+ }
+ } else if (intent.getAction().equals(SOS.ACTION_SOS)) {
+ LogUtils.d(TAG, "ACTION_SOS");
+ LogUtils.d(TAG, String.format("context.getPackageName() %s", context.getPackageName()));
+ LogUtils.d(TAG, String.format("intent.getAction() %s", intent.getAction()));
+ String SOS = intent.getStringExtra("SOS");
+ LogUtils.d(TAG, String.format("SOS %s", SOS));
+ if (SOS != null && SOS.equals("Service")) {
+ String szAPPSOSBean = intent.getStringExtra("APPSOSBean");
+ LogUtils.d(TAG, String.format("szAPPSOSBean %s", szAPPSOSBean));
+ if (szAPPSOSBean != null && !szAPPSOSBean.equals("")) {
+ try {
+ APPSOSBean bean = APPSOSBean.parseStringToBean(szAPPSOSBean, APPSOSBean.class);
+ if (bean != null) {
+ String sosPackage = bean.getSosPackage();
+ LogUtils.d(TAG, String.format("sosPackage %s", sosPackage));
+ String sosClassName = bean.getSosClassName();
+ LogUtils.d(TAG, String.format("sosClassName %s", sosClassName));
+
+ Intent intentService = new Intent();
+ intentService.setComponent(new ComponentName(sosPackage, sosClassName));
+ context.startService(intentService);
+
+ String appName = AppUtils.getAppNameByPackageName(context, sosPackage);
+ LogUtils.d(TAG, String.format("appName %s", appName));
+ SOSReportBean appSOSReportBean = new SOSReportBean(appName);
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
+ String currentTime = sdf.format(new Date());
+ StringBuilder sbLine = new StringBuilder();
+ sbLine.append("[");
+ sbLine.append(currentTime);
+ sbLine.append("] Power to ");
+ sbLine.append(appName);
+ appSOSReportBean.setSosReport(sbLine.toString());
+
+ SOSWidget.addAPPSOSReportBean(context, appSOSReportBean);
+ Intent intentWidget = new Intent(context, SOSWidget.class);
+ intentWidget.setAction(SOSWidget.ACTION_RELOAD_REPORT);
+ context.sendBroadcast(intentWidget);
+ }
+ } catch (IOException e) {
+ LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
+ }
+ }
+ }
} else {
ToastUtils.show(szAction);
}
@@ -39,10 +118,14 @@ public class MainReceiver extends BroadcastReceiver {
// 注册 Receiver
//
- public void registerAction(Context context) {
+ public void registerAction(MainService service) {
IntentFilter filter=new IntentFilter();
filter.addAction(ACTION_BOOT_COMPLETED);
+ filter.addAction(SOS.ACTION_SOS);
+ filter.addAction(SOS.ACTION_BIND);
+ filter.addAction(SOS.ACTION_SERVICE_ENABLE);
+ filter.addAction(SOS.ACTION_SERVICE_DISABLE);
//filter.addAction(Intent.ACTION_BATTERY_CHANGED);
- context.registerReceiver(this, filter);
+ service.registerReceiver(this, filter);
}
}
diff --git a/appbase/src/main/java/cc/winboll/studio/appbase/services/MainService.java b/appbase/src/main/java/cc/winboll/studio/appbase/services/MainService.java
index ce66f5c..50c2f9d 100644
--- a/appbase/src/main/java/cc/winboll/studio/appbase/services/MainService.java
+++ b/appbase/src/main/java/cc/winboll/studio/appbase/services/MainService.java
@@ -23,8 +23,10 @@ import cc.winboll.studio.appbase.handlers.MainServiceHandler;
import cc.winboll.studio.appbase.receivers.MainReceiver;
import cc.winboll.studio.appbase.services.AssistantService;
import cc.winboll.studio.appbase.threads.MainServiceThread;
-import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.appbase.widgets.SOSWidget;
+import cc.winboll.studio.libappbase.LogUtils;
+import cc.winboll.studio.libappbase.bean.APPSOSBean;
+import java.util.ArrayList;
public class MainService extends Service {
@@ -43,6 +45,7 @@ public class MainService extends Service {
AssistantService mAssistantService;
boolean isBound = false;
MainReceiver mMainReceiver;
+ ArrayList mSOSConnectionList;
@Override
public IBinder onBind(Intent intent) {
@@ -57,6 +60,8 @@ public class MainService extends Service {
public void onCreate() {
super.onCreate();
LogUtils.d(TAG, "onCreate()");
+ mSOSConnectionList = new ArrayList();
+
_mControlCenterService = MainService.this;
isServiceRunning = false;
mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class);
@@ -94,16 +99,16 @@ public class MainService extends Service {
mMainReceiver = new MainReceiver(this);
mMainReceiver.registerAction(this);
}
-
+
// 启动小部件
Intent intentTimeWidget = new Intent(this, SOSWidget.class);
intentTimeWidget.setAction(SOSWidget.ACTION_RELOAD_REPORT);
this.sendBroadcast(intentTimeWidget);
-
+
startMainServiceThread();
MyTileService.updateServiceIconStatus(this);
-
+
LogUtils.i(TAG, "Main Service Is Start.");
}
}
@@ -181,12 +186,67 @@ public class MainService extends Service {
stopRemindThread();
MyTileService.updateServiceIconStatus(this);
-
+
super.onDestroy();
//LogUtils.d(TAG, "onDestroy done");
}
}
+ public void bindSOSConnection(APPSOSBean bean) {
+ LogUtils.d(TAG, "bindSOSConnection(...)");
+ // 清理旧的绑定链接
+ for (int i = mSOSConnectionList.size() - 1; i > -1; i--) {
+ SOSConnection item = mSOSConnectionList.get(i);
+ if (item.isBindToAPPSOSBean(bean)) {
+ LogUtils.d(TAG, "Bind Servive exist.");
+ unbindService(item);
+ mSOSConnectionList.remove(i);
+ }
+ }
+
+ // 绑定服务
+ SOSConnection sosConnection = new SOSConnection();
+ Intent intentService = new Intent();
+ intentService.setComponent(new ComponentName(bean.getSosPackage(), bean.getSosClassName()));
+ bindService(intentService, sosConnection, Context.BIND_IMPORTANT);
+ mSOSConnectionList.add(sosConnection);
+ }
+
+ public class SOSConnection implements ServiceConnection {
+
+ ComponentName mComponentName;
+
+ boolean isBindToAPPSOSBean(APPSOSBean bean) {
+ return mComponentName != null
+ && mComponentName.getClassName().equals(bean.getSosClassName())
+ && mComponentName.getPackageName().equals(bean.getSosPackage());
+ }
+
+ @Override
+ public void onServiceConnected(ComponentName name, IBinder service) {
+ LogUtils.d(TAG, "onServiceConnected(...)");
+ mComponentName = name;
+ LogUtils.d(TAG, String.format("onServiceConnected : \ngetClassName %s\ngetPackageName %s", name.getClassName(), name.getPackageName()));
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName name) {
+ LogUtils.d(TAG, "onServiceDisconnected(...)");
+ LogUtils.d(TAG, String.format("onServiceDisconnected : \ngetClassName %s\ngetPackageName %s", name.getClassName(), name.getPackageName()));
+
+ // 尝试无参数启动一下服务
+ String sosPackage = mComponentName.getPackageName();
+ LogUtils.d(TAG, String.format("sosPackage %s", sosPackage));
+ String sosClassName = mComponentName.getClassName();
+ LogUtils.d(TAG, String.format("sosClassName %s", sosClassName));
+
+ Intent intentService = new Intent();
+ intentService.setComponent(new ComponentName(sosPackage, sosClassName));
+ startService(intentService);
+ }
+
+ }
+
// 主进程与守护进程连接时需要用到此类
//
private class MyServiceConnection implements ServiceConnection {
diff --git a/appbase/src/main/java/cc/winboll/studio/appbase/widgets/SOSWidget.java b/appbase/src/main/java/cc/winboll/studio/appbase/widgets/SOSWidget.java
index b10de45..fef8ad4 100644
--- a/appbase/src/main/java/cc/winboll/studio/appbase/widgets/SOSWidget.java
+++ b/appbase/src/main/java/cc/winboll/studio/appbase/widgets/SOSWidget.java
@@ -46,53 +46,7 @@ public class SOSWidget extends AppWidgetProvider {
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
initAPPSOSReportBeanList(context);
- if (intent.getAction().equals(ACTION_SOS)) {
- LogUtils.d(TAG, "ACTION_SOS");
- LogUtils.d(TAG, String.format("context.getPackageName() %s", context.getPackageName()));
- LogUtils.d(TAG, String.format("intent.getAction() %s", intent.getAction()));
- String SOS = intent.getStringExtra("SOS");
- LogUtils.d(TAG, String.format("SOS %s", SOS));
- if (SOS != null && SOS.equals("Service")) {
- String szAPPSOSBean = intent.getStringExtra("APPSOSBean");
- LogUtils.d(TAG, String.format("szAPPSOSBean %s", szAPPSOSBean));
- if (szAPPSOSBean != null && !szAPPSOSBean.equals("")) {
- try {
- APPSOSBean bean = APPSOSBean.parseStringToBean(szAPPSOSBean, APPSOSBean.class);
- if (bean != null) {
- String sosPackage = bean.getSosPackage();
- LogUtils.d(TAG, String.format("sosPackage %s", sosPackage));
- String sosClassName = bean.getSosClassName();
- LogUtils.d(TAG, String.format("sosClassName %s", sosClassName));
-
- Intent intentService = new Intent();
- intentService.setComponent(new ComponentName(sosPackage, sosClassName));
- context.startService(intentService);
-
- String appName = AppUtils.getAppNameByPackageName(context, sosPackage);
- LogUtils.d(TAG, String.format("appName %s", appName));
- SOSReportBean appSOSReportBean = new SOSReportBean(appName);
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
- String currentTime = sdf.format(new Date());
- StringBuilder sbLine = new StringBuilder();
- sbLine.append("[");
- sbLine.append(currentTime);
- sbLine.append("] Power to ");
- sbLine.append(appName);
- appSOSReportBean.setSosReport(sbLine.toString());
- addAPPSOSReportBean(context, appSOSReportBean);
-
- AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
- int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, SOSWidget.class));
- for (int appWidgetId : appWidgetIds) {
- updateAppWidget(context, appWidgetManager, appWidgetId);
- }
- }
- } catch (IOException e) {
- LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
- }
- }
- }
- } else if (intent.getAction().equals(ACTION_RELOAD_REPORT)) {
+ if (intent.getAction().equals(ACTION_RELOAD_REPORT)) {
LogUtils.d(TAG, "ACTION_RELOAD_REPORT");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, SOSWidget.class));
@@ -105,7 +59,7 @@ public class SOSWidget extends AppWidgetProvider {
//
// 加入新报告信息
//
- void addAPPSOSReportBean(Context context, SOSReportBean bean) {
+ public synchronized static void addAPPSOSReportBean(Context context, SOSReportBean bean) {
initAPPSOSReportBeanList(context);
_SOSReportBeanList.add(0, bean);
// 控制记录总数
@@ -115,7 +69,7 @@ public class SOSWidget extends AppWidgetProvider {
SOSReportBean.saveBeanList(context, _SOSReportBeanList, SOSReportBean.class);
}
- void initAPPSOSReportBeanList(Context context) {
+ synchronized static void initAPPSOSReportBeanList(Context context) {
if (_SOSReportBeanList == null) {
_SOSReportBeanList = new ArrayList();
SOSReportBean.loadBeanList(context, _SOSReportBeanList, SOSReportBean.class);
@@ -150,7 +104,7 @@ public class SOSWidget extends AppWidgetProvider {
if (_SOSReportBeanList != null) {
int start = _OnePageLinesCount * _CurrentPageIndex;
start = _SOSReportBeanList.size() > start ? start : _SOSReportBeanList.size() - 1;
- for (int i = start, j = 0; i < _SOSReportBeanList.size() && j < _OnePageLinesCount; i++, j++) {
+ for (int i = start, j = 0; i < _SOSReportBeanList.size() && j < _OnePageLinesCount && start > -1; i++, j++) {
msgTemp.add(_SOSReportBeanList.get(i).getSosReport());
}
String message = String.join("\n", msgTemp);
diff --git a/appbase/src/main/res/layout/activity_main.xml b/appbase/src/main/res/layout/activity_main.xml
index 56944c4..7414106 100644
--- a/appbase/src/main/res/layout/activity_main.xml
+++ b/appbase/src/main/res/layout/activity_main.xml
@@ -83,9 +83,9 @@
+ android:onClick="onStopTestServiceNoSettings"/>
diff --git a/libappbase/build.properties b/libappbase/build.properties
index d7d7f0e..4b5cf0d 100644
--- a/libappbase/build.properties
+++ b/libappbase/build.properties
@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
-#Wed Feb 19 03:18:52 HKT 2025
+#Wed Feb 19 11:49:56 GMT 2025
stageCount=4
libraryProject=libappbase
baseVersion=1.5
publishVersion=1.5.3
-buildCount=0
+buildCount=61
baseBetaVersion=1.5.4
diff --git a/libappbase/src/main/java/cc/winboll/studio/libappbase/SOS.java b/libappbase/src/main/java/cc/winboll/studio/libappbase/SOS.java
index e9524e7..0cb730e 100644
--- a/libappbase/src/main/java/cc/winboll/studio/libappbase/SOS.java
+++ b/libappbase/src/main/java/cc/winboll/studio/libappbase/SOS.java
@@ -5,15 +5,22 @@ package cc.winboll.studio.libappbase;
* @Date 2025/02/13 21:09:36
* @Describe SOS 组件
*/
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+import android.content.ServiceConnection;
+import android.os.IBinder;
import cc.winboll.studio.libappbase.bean.APPSOSBean;
+import java.util.ArrayList;
public class SOS {
public static final String TAG = "SOS";
- public static final String ACTION_SOS = "cc.winboll.studio.appbase.widgets.SOSWidget.ACTION_SOS";
+ public static final String ACTION_SOS = SOS.class.getName() + ".ACTION_SOS";
+ public static final String ACTION_BIND = SOS.class.getName() + ".ACTION_BIND";
+ public static final String ACTION_SERVICE_ENABLE = SOS.class.getName() + ".ACTION_SERVICE_ENABLE";
+ public static final String ACTION_SERVICE_DISABLE = SOS.class.getName() + ".ACTION_SERVICE_DISENABLE";
public static void sosWinBollService(Context context, APPSOSBean bean) {
Intent intent = new Intent(ACTION_SOS);
@@ -28,8 +35,23 @@ public class SOS {
intent.setPackage(szToPackage);
context.sendBroadcast(intent);
- LogUtils.d(TAG, String.format("SOS Send To WinBoll. (szToPackage : %s)", szToPackage));
+ LogUtils.d(TAG, String.format("Send ACTION_SOS To WinBoll. (szToPackage : %s)", szToPackage));
//ToastUtils.show("SOS Send To WinBoll");
}
+ public static void bindToAPPService(Context context, APPSOSBean bean) {
+ Intent intent = new Intent(ACTION_BIND);
+ intent.putExtra("SOS", "Service");
+ intent.putExtra("APPSOSBean", bean.toString());
+ String szToPackage = "";
+ if (GlobalApplication.isDebuging()) {
+ szToPackage = "cc.winboll.studio.appbase.beta";
+ } else {
+ szToPackage = "cc.winboll.studio.appbase";
+ }
+ intent.setPackage(szToPackage);
+ context.sendBroadcast(intent);
+ LogUtils.d(TAG, String.format("Send ACTION_BIND To WinBoll. (szToPackage : %s)", szToPackage));
+ }
+
}
diff --git a/libappbase/src/main/java/cc/winboll/studio/libappbase/bean/SimpleOperateSignalCenterServiceBean.java b/libappbase/src/main/java/cc/winboll/studio/libappbase/bean/SimpleOperateSignalCenterServiceBean.java
index c9b3795..d6d3f00 100644
--- a/libappbase/src/main/java/cc/winboll/studio/libappbase/bean/SimpleOperateSignalCenterServiceBean.java
+++ b/libappbase/src/main/java/cc/winboll/studio/libappbase/bean/SimpleOperateSignalCenterServiceBean.java
@@ -34,8 +34,7 @@ public class SimpleOperateSignalCenterServiceBean extends BaseBean {
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
- SimpleOperateSignalCenterServiceBean bean = this;
- jsonWriter.name("isEnable").value(bean.isEnable());
+ jsonWriter.name("isEnable").value(isEnable());
}
diff --git a/libappbase/src/main/java/cc/winboll/studio/libappbase/bean/TestServiceBean.java b/libappbase/src/main/java/cc/winboll/studio/libappbase/bean/TestServiceBean.java
new file mode 100644
index 0000000..30d8cfe
--- /dev/null
+++ b/libappbase/src/main/java/cc/winboll/studio/libappbase/bean/TestServiceBean.java
@@ -0,0 +1,67 @@
+package cc.winboll.studio.libappbase.bean;
+
+/**
+ * @Author ZhanGSKen@AliYun.Com
+ * @Date 2025/02/19 13:34:52
+ * @Describe TestServiceBean
+ */
+import android.util.JsonReader;
+import android.util.JsonWriter;
+import cc.winboll.studio.libappbase.BaseBean;
+import java.io.IOException;
+
+public class TestServiceBean extends BaseBean {
+
+ public static final String TAG = "TestServiceBean";
+
+ boolean isEnable;
+
+ public TestServiceBean() {
+ this.isEnable = false;
+ }
+
+ public void setIsEnable(boolean isEnable) {
+ this.isEnable = isEnable;
+ }
+
+ public boolean isEnable() {
+ return isEnable;
+ }
+
+ @Override
+ public String getName() {
+ return TestServiceBean.class.getName();
+ }
+
+ @Override
+ public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
+ super.writeThisToJsonWriter(jsonWriter);
+ jsonWriter.name("isEnable").value(isEnable());
+ }
+
+ @Override
+ public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException {
+ if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else {
+ if (name.equals("isEnable")) {
+ setIsEnable(jsonReader.nextBoolean());
+ } else {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
+ jsonReader.beginObject();
+ while (jsonReader.hasNext()) {
+ String name = jsonReader.nextName();
+ if (!initObjectsFromJsonReader(jsonReader, name)) {
+ jsonReader.skipValue();
+ }
+ }
+ // 结束 JSON 对象
+ jsonReader.endObject();
+ return this;
+ }
+}
diff --git a/libappbase/src/main/java/cc/winboll/studio/libappbase/services/TestService.java b/libappbase/src/main/java/cc/winboll/studio/libappbase/services/TestService.java
index 881032f..fc86253 100644
--- a/libappbase/src/main/java/cc/winboll/studio/libappbase/services/TestService.java
+++ b/libappbase/src/main/java/cc/winboll/studio/libappbase/services/TestService.java
@@ -11,14 +11,26 @@ import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import cc.winboll.studio.libappbase.LogUtils;
-import cc.winboll.studio.libappbase.widgets.StatusWidget;
+import cc.winboll.studio.libappbase.SOS;
+import cc.winboll.studio.libappbase.bean.APPSOSBean;
+import cc.winboll.studio.libappbase.bean.TestServiceBean;
public class TestService extends Service {
public static final String TAG = "TestService";
- TestThread mTestThread;
-
+ volatile static TestThread _TestThread;
+
+ volatile static boolean _IsRunning;
+
+ public synchronized static void setIsRunning(boolean isRunning) {
+ _IsRunning = isRunning;
+ }
+
+ public static boolean isRunning() {
+ return _IsRunning;
+ }
+
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
@@ -34,27 +46,77 @@ public class TestService extends Service {
public void onCreate() {
super.onCreate();
LogUtils.d(TAG, "onCreate()");
- mTestThread = new TestThread();
- mTestThread.start();
+
+
+ run();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtils.d(TAG, "onStartCommand(...)");
- return super.onStartCommand(intent, flags, startId);
+ TestServiceBean bean = TestServiceBean.loadBean(this, TestServiceBean.class);
+ if (bean == null) {
+ bean = new TestServiceBean();
+ }
+ if (intent.getAction() != null && intent.getAction().equals(SOS.ACTION_SERVICE_ENABLE)) {
+ bean.setIsEnable(true);
+ TestServiceBean.saveBean(this, bean);
+ run();
+ } else if (intent.getAction() != null && intent.getAction().equals(SOS.ACTION_SERVICE_DISABLE)) {
+ bean.setIsEnable(false);
+ TestServiceBean.saveBean(this, bean);
+ }
+ LogUtils.d(TAG, String.format("TestServiceBean.saveBean setIsEnable %s", bean.isEnable()));
+ return (bean.isEnable()) ? START_STICKY : super.onStartCommand(intent, flags, startId);
+ //return super.onStartCommand(intent, flags, startId);
}
+ void run() {
+ LogUtils.d(TAG, "run()");
+ TestServiceBean bean = TestServiceBean.loadBean(this, TestServiceBean.class);
+ if (bean == null) {
+ bean = new TestServiceBean();
+ TestServiceBean.saveBean(this, bean);
+ }
+ if (bean.isEnable()) {
+ LogUtils.d(TAG, "run() bean.isEnable()");
+ TestThread.getInstance(this).start();
+ LogUtils.d(TAG, "_TestThread.start()");
+ }
+ }
+
+
@Override
public void onDestroy() {
super.onDestroy();
LogUtils.d(TAG, "onDestroy()");
- mTestThread.setIsExit(true);
+ TestThread.getInstance(this).setIsExit(true);
+
+ _IsRunning = false;
}
- class TestThread extends Thread {
+ static class TestThread extends Thread {
+
+ volatile static TestThread _TestThread;
+ Context mContext;
+ volatile boolean isStarted = false;
volatile boolean isExit = false;
- public void setIsExit(boolean isExit) {
+ TestThread(Context context) {
+ super();
+ mContext = context;
+ }
+
+ public static synchronized TestThread getInstance(Context context) {
+ if (_TestThread != null) {
+ _TestThread.setIsExit(true);
+ }
+ _TestThread = new TestThread(context);
+
+ return _TestThread;
+ }
+
+ public synchronized void setIsExit(boolean isExit) {
this.isExit = isExit;
}
@@ -64,30 +126,24 @@ public class TestService extends Service {
@Override
public void run() {
- super.run();
+ if (isStarted == false) {
+ isStarted = true;
+ super.run();
+ LogUtils.d(TAG, "run() start");
+ SOS.bindToAPPService(mContext, new APPSOSBean(mContext.getPackageName(), TestService.class.getName()));
- LogUtils.d(TAG, "run() start");
- Intent intentStart = new Intent(TestService.this, StatusWidget.class);
- intentStart.setAction(StatusWidget.ACTION_STATUS_UPDATE);
- sendBroadcast(intentStart);
+ while (!isExit()) {
+ LogUtils.d(TAG, "run()");
- while (!isExit) {
- //LogUtils.d(TAG, "run()");
-
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
+ }
}
+
+ LogUtils.d(TAG, "run() exit");
}
-
- Intent intentStop = new Intent(TestService.this, StatusWidget.class);
- intentStop.setAction(StatusWidget.ACTION_STATUS_UPDATE);
- sendBroadcast(intentStop);
-
- LogUtils.d(TAG, "run() exit");
}
-
}
-
}