Skip to content

Commit

Permalink
增加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed Aug 27, 2019
1 parent b1e5a00 commit 8ecf061
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
3 changes: 3 additions & 0 deletions keeplive/src/main/java/com/xuexiang/keeplive/KeepLive.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

/**
* 保活工具
*
* @author xuexiang
* @since 2019-08-27 10:01
*/
public final class KeepLive {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@

import com.xuexiang.keeplive.activity.OnePixelActivity;

/**
* 一像素保活方案
*
* @author xuexiang
* @since 2019-08-27 9:32
*/
public final class OnePxReceiver extends BroadcastReceiver {
public static final String KEEP_ACTION_SCREEN_OFF = "com.xuexiang.keeplive.receiver._ACTION_SCREEN_OFF";
public static final String KEEP_ACTION_SCREEN_ON = "com.xuexiang.keeplive.receiver._ACTION_SCREEN_ON";
public static final String KEEP_ACTION_SCREEN_OFF = "com.xuexiang.keeplive.receiver.KEEP_ACTION_SCREEN_OFF";
public static final String KEEP_ACTION_SCREEN_ON = "com.xuexiang.keeplive.receiver.KEEP_ACTION_SCREEN_ON";

private Handler mHandler;
private boolean mScreenOn = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
import static com.xuexiang.keeplive.utils.NotificationUtils.KEY_NOTIFICATION_ID;

/**
* 隐藏前台服务通知
* 通过相同的notification_id来隐藏前台服务通知
*
* @author xuexiang
* @since 2019-08-27 9:33
*/
public class HideForegroundService extends Service {

private Handler mHandler;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static com.xuexiang.keeplive.service.LocalService.KEY_LOCAL_SERVICE_NAME;

/**
* 定时器
* 定时器保活
* 安卓5.0及以上
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
Expand All @@ -35,15 +35,19 @@ public int onStartCommand(Intent intent, int flags, int startId) {
mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(startId++, new ComponentName(getPackageName(), JobHandlerService.class.getName()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS); //执行的最小延迟时间
builder.setOverrideDeadline(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS); //执行的最长延时时间
//执行的最小延迟时间
builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);
builder.setBackoffCriteria(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS, JobInfo.BACKOFF_POLICY_LINEAR);//线性重试方案
//执行的最长延时时间
builder.setOverrideDeadline(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);
builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);
//线性重试方案
builder.setBackoffCriteria(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS, JobInfo.BACKOFF_POLICY_LINEAR);
} else {
builder.setPeriodic(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);
}
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setRequiresCharging(true); // 当插入充电器,执行该任务
// 当插入充电器,执行该任务
builder.setRequiresCharging(true);
mJobScheduler.schedule(builder.build());
}
return START_STICKY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

import com.xuexiang.keeplive.KeepLive;
import com.xuexiang.keeplive.R;
import com.xuexiang.keeplive.utils.NotificationUtils;
import com.xuexiang.keeplive.receiver.NotificationClickReceiver;
import com.xuexiang.keeplive.receiver.OnePxReceiver;
import com.xuexiang.keeplive.utils.NotificationUtils;
import com.xuexiang.keeplive.utils.ServiceUtils;

import static com.xuexiang.keeplive.utils.NotificationUtils.KEY_NOTIFICATION_ID;
import static com.xuexiang.keeplive.receiver.OnePxReceiver.KEEP_ACTION_SCREEN_OFF;
import static com.xuexiang.keeplive.receiver.OnePxReceiver.KEEP_ACTION_SCREEN_ON;
import static com.xuexiang.keeplive.utils.NotificationUtils.KEY_NOTIFICATION_ID;

/**
* 本地服务
* 本地服务(双进程守护之本地进程)
*
* @author xuexiang
* @since 2019-08-18 23:23
Expand All @@ -41,6 +41,9 @@ public final class LocalService extends Service {
* 控制暂停
*/
private boolean mIsPause = true;
/**
* 无声音乐保活
*/
private MediaPlayer mMediaPlayer;
private GuardBinder mBinder;
private Handler mHandler;
Expand Down Expand Up @@ -127,9 +130,7 @@ public void run() {
//隐藏服务通知
try {
if (KeepLive.foregroundNotification == null || !KeepLive.foregroundNotification.isShow()) {
if (Build.VERSION.SDK_INT < 25) {
startService(new Intent(this, HideForegroundService.class));
}
startService(new Intent(this, HideForegroundService.class));
}
} catch (Exception e) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static com.xuexiang.keeplive.receiver.OnePxReceiver.KEEP_ACTION_SCREEN_ON;

/**
* 守护进程服务
* 守护进程服务(双进程守护之守护进程)
*
* @author xuexiang
* @since 2019-08-14 16:09
Expand Down Expand Up @@ -69,12 +69,10 @@ private final class GuardBinder extends GuardAidl.Stub {

@Override
public void wakeUp(String title, String description, int iconRes) throws RemoteException {
if (Build.VERSION.SDK_INT < 25) {
Intent intent2 = new Intent(getApplicationContext(), NotificationClickReceiver.class);
intent2.setAction(NotificationClickReceiver.ACTION_CLICK_NOTIFICATION);
Notification notification = NotificationUtils.createNotification(RemoteService.this, title, description, iconRes, intent2);
RemoteService.this.startForeground(KEY_NOTIFICATION_ID, notification);
}
Intent intent = new Intent(getApplicationContext(), NotificationClickReceiver.class);
intent.setAction(NotificationClickReceiver.ACTION_CLICK_NOTIFICATION);
Notification notification = NotificationUtils.createNotification(RemoteService.this, title, description, iconRes, intent);
RemoteService.this.startForeground(KEY_NOTIFICATION_ID, notification);
}

}
Expand All @@ -83,11 +81,9 @@ public void wakeUp(String title, String description, int iconRes) throws RemoteE
@Override
public void onServiceDisconnected(ComponentName name) {
if (ServiceUtils.isRunningTaskExist(getApplicationContext(), getPackageName() + ":remote")) {
Intent localService = new Intent(RemoteService.this,
LocalService.class);
Intent localService = new Intent(RemoteService.this, LocalService.class);
RemoteService.this.startService(localService);
mIsBoundLocalService = RemoteService.this.bindService(new Intent(RemoteService.this,
LocalService.class), mConnection, Context.BIND_ABOVE_CLIENT);
mIsBoundLocalService = RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), mConnection, Context.BIND_ABOVE_CLIENT);
}
PowerManager pm = (PowerManager) RemoteService.this.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm != null && pm.isScreenOn();
Expand Down

0 comments on commit 8ecf061

Please sign in to comment.