Skip to content

Commit 038027b

Browse files
author
liuhuo.xd
committed
1.2.2 ver: Bug 修复.
1 parent 83e8bb4 commit 038027b

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

hellodaemon/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ext {
1313
siteUrl = 'https://github.com/xingda920813/HelloDaemon'
1414
gitUrl = 'https://github.com/xingda920813/HelloDaemon.git'
1515

16-
libraryVersion = '1.2.1'
16+
libraryVersion = '1.2.2'
1717

1818
developerId = 'xingda920813'
1919
developerName = 'Da Xing'
@@ -31,8 +31,8 @@ android {
3131
defaultConfig {
3232
minSdkVersion 14
3333
targetSdkVersion 26
34-
versionCode 10
35-
versionName "1.2.1"
34+
versionCode 11
35+
versionName "1.2.2"
3636
}
3737
buildTypes {
3838
release {

hellodaemon/src/main/java/com/xdandroid/hellodaemon/AbsWorkService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void cancelJobAlarmSub() {
3636
public abstract void onServiceKilled(Intent rootIntent);
3737

3838
/**
39-
* 1.防止重复启动,可以任意调用startService(Intent i);
39+
* 1.防止重复启动,可以任意调用 DaemonEnv.startServiceMayBind(Class serviceClass);
4040
* 2.利用漏洞启动前台服务而不显示通知;
4141
* 3.在子线程中运行定时任务,处理了运行前检查和销毁时保存的问题;
4242
* 4.启动守护服务;

hellodaemon/src/main/java/com/xdandroid/hellodaemon/DaemonEnv.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ private DaemonEnv() {}
1414
public static final int DEFAULT_WAKE_UP_INTERVAL = 6 * 60 * 1000;
1515
private static final int MINIMAL_WAKE_UP_INTERVAL = 3 * 60 * 1000;
1616

17-
static final Map<Class<? extends Service>, Boolean> BIND_STATE_MAP = new HashMap<>();
18-
1917
static Context sApp;
2018
static Class<? extends AbsWorkService> sServiceClass;
2119
private static int sWakeUpInterval = DEFAULT_WAKE_UP_INTERVAL;
2220
static boolean sInitialized;
2321

22+
static final Map<Class<? extends Service>, ServiceConnection> BIND_STATE_MAP = new HashMap<>();
23+
2424
/**
2525
* @param app Application Context.
2626
* @param wakeUpInterval 定时唤醒的时间间隔(ms).
@@ -36,24 +36,24 @@ public static void startServiceMayBind(@NonNull final Class<? extends Service> s
3636
if (!sInitialized) return;
3737
final Intent i = new Intent(sApp, serviceClass);
3838
startServiceSafely(i);
39-
Boolean bound = BIND_STATE_MAP.get(serviceClass);
40-
if (bound == null || !bound) sApp.bindService(i, new ServiceConnection() {
39+
ServiceConnection bound = BIND_STATE_MAP.get(serviceClass);
40+
if (bound == null) sApp.bindService(i, new ServiceConnection() {
4141
@Override
4242
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
43-
BIND_STATE_MAP.put(serviceClass, true);
43+
BIND_STATE_MAP.put(serviceClass, this);
4444
}
4545

4646
@Override
4747
public void onServiceDisconnected(ComponentName componentName) {
48-
BIND_STATE_MAP.put(serviceClass, false);
48+
BIND_STATE_MAP.remove(serviceClass);
4949
startServiceSafely(i);
5050
if (!sInitialized) return;
5151
sApp.bindService(i, this, Context.BIND_AUTO_CREATE);
5252
}
5353
}, Context.BIND_AUTO_CREATE);
5454
}
5555

56-
public static void startServiceSafely(Intent i) {
56+
static void startServiceSafely(Intent i) {
5757
if (!sInitialized) return;
5858
try { sApp.startService(i); } catch (Exception ignored) {}
5959
}

hellodaemon/src/main/java/com/xdandroid/hellodaemon/IntentWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ protected boolean doesActivityExists() {
430430
/**
431431
* 安全地启动一个Activity
432432
*/
433-
public void startActivitySafely(Activity activityContext) {
433+
protected void startActivitySafely(Activity activityContext) {
434434
try { activityContext.startActivity(intent); } catch (Exception e) { e.printStackTrace(); }
435435
}
436436
}

sample/src/main/java/com/xdandroid/sample/App.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.xdandroid.sample;
22

33
import android.app.*;
4-
import android.content.*;
54

65
import com.xdandroid.hellodaemon.*;
76

@@ -12,6 +11,7 @@ public void onCreate() {
1211
super.onCreate();
1312
//需要在 Application 的 onCreate() 中调用一次 DaemonEnv.initialize()
1413
DaemonEnv.initialize(this, TraceServiceImpl.class, DaemonEnv.DEFAULT_WAKE_UP_INTERVAL);
15-
DaemonEnv.startServiceSafely(new Intent(this, TraceServiceImpl.class));
14+
TraceServiceImpl.sShouldStopService = false;
15+
DaemonEnv.startServiceMayBind(TraceServiceImpl.class);
1616
}
1717
}

sample/src/main/java/com/xdandroid/sample/MainActivity.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.xdandroid.sample;
22

33
import android.app.*;
4-
import android.content.*;
54
import android.os.*;
65
import android.view.*;
76

@@ -16,12 +15,21 @@ protected void onCreate(Bundle b) {
1615

1716
public void onClick(View v) {
1817
switch (v.getId()) {
19-
case R.id.btn_start: DaemonEnv.startServiceSafely(new Intent(this, TraceServiceImpl.class)); break;
20-
case R.id.btn_white: IntentWrapper.whiteListMatters(this, "轨迹跟踪服务的持续运行"); break;
21-
case R.id.btn_stop: TraceServiceImpl.stopService(); break;
18+
case R.id.btn_start:
19+
TraceServiceImpl.sShouldStopService = false;
20+
DaemonEnv.startServiceMayBind(TraceServiceImpl.class);
21+
break;
22+
case R.id.btn_white:
23+
IntentWrapper.whiteListMatters(this, "轨迹跟踪服务的持续运行");
24+
break;
25+
case R.id.btn_stop:
26+
TraceServiceImpl.stopService();
27+
break;
2228
}
2329
}
2430

2531
//防止华为机型未加入白名单时按返回键回到桌面再锁屏后几秒钟进程被杀
26-
@Override public void onBackPressed() { IntentWrapper.onBackPressed(this); }
32+
public void onBackPressed() {
33+
IntentWrapper.onBackPressed(this);
34+
}
2735
}

sample/src/main/java/com/xdandroid/sample/TraceServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ public void startWork(Intent intent, int flags, int startId) {
4040
sDisposable = Flowable
4141
.interval(3, TimeUnit.SECONDS)
4242
//取消任务时取消定时唤醒
43-
.doOnTerminate(() -> {
43+
.doOnCancel(() -> {
4444
System.out.println("保存数据到磁盘。");
4545
cancelJobAlarmSub();
46-
}).subscribe(count -> {
46+
})
47+
.subscribe(count -> {
4748
System.out.println("每 3 秒采集一次数据... count = " + count);
4849
if (count > 0 && count % 18 == 0) System.out.println("保存数据到磁盘。 saveCount = " + (count / 18 - 1));
4950
});

0 commit comments

Comments
 (0)