Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed Sep 15, 2019
1 parent 0304a65 commit 4aea0e4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 65 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/com/xuexiang/pushdemo/MyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import com.xuexiang.xpush.core.dispatcher.impl.Android26PushDispatcherImpl;
import com.xuexiang.xpush.huawei.HuaweiPushClient;
import com.xuexiang.xpush.jpush.JPushClient;
import com.xuexiang.xpush.umeng.UMengPushClient;
import com.xuexiang.xpush.xiaomi.XiaoMiPushClient;
import com.xuexiang.xutil.XUtil;
import com.xuexiang.xutil.app.AppUtils;
Expand Down
1 change: 0 additions & 1 deletion keeplive/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:theme="@style/OnePixelActivity" />

<service android:name=".service.LocalService" />
<service android:name=".service.HideForegroundService" />
<service
Expand Down
20 changes: 1 addition & 19 deletions keeplive/src/main/java/com/xuexiang/keeplive/KeepLive.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
Expand Down Expand Up @@ -60,8 +59,6 @@ public enum RunMode {
* @param keepLiveService 保活业务
*/
public static void startWork(@NonNull Application application, @NonNull RunMode runMode, @NonNull ForegroundNotification foregroundNotification, @NonNull KeepLiveService keepLiveService) {
setIsKeepLive(application, true);

if (ServiceUtils.isMainProcess(application)) {
KeepLive.sApplication = application;
KeepLive.sForegroundNotification = foregroundNotification;
Expand Down Expand Up @@ -94,21 +91,18 @@ public static void stopWork() {
* @param context
*/
public static void stopWork(@NonNull Context context) {
setIsKeepLive(context, false);

if (KeepLive.sForegroundNotification != null && KeepLive.sForegroundNotification.isShow()) {
context.startService(new Intent(context, HideForegroundService.class));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//停止定时器,在定时器中启动本地服务和守护进程
context.stopService(new Intent(context, JobHandlerService.class));
JobHandlerService.stop(context);
} else {
stopDoubleProcessService(context);
}
}


public static void startDoubleProcessService(@NonNull Context context) {
//启动本地服务
Intent localIntent = new Intent(context, LocalService.class);
Expand All @@ -134,18 +128,6 @@ public static Application getApplication() {
return sApplication;
}

public static boolean isKeepLive(Context context) {
return getKeepLiveSP(context).getBoolean("key_is_KeepLive", false);
}

public static SharedPreferences getKeepLiveSP(Context context) {
return context.getSharedPreferences("KeepLive", Context.MODE_MULTI_PROCESS | Context.MODE_PRIVATE);
}

public static void setIsKeepLive(Context context, boolean isKeepLive) {
getKeepLiveSP(context).edit().putBoolean("key_is_KeepLive", isKeepLive).apply();
}

/**
* 是否启用无声音乐
* 如不设置,则默认启用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;

import com.xuexiang.keeplive.KeepLive;
Expand All @@ -26,12 +27,28 @@
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public final class JobHandlerService extends JobService {

private static final int JOB_SERVICE_ID = 11000;

/**
* 停止保活服务
*
* @param context
*/
public static void stop(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.cancel(JOB_SERVICE_ID);
}
context.stopService(new Intent(context, JobHandlerService.class));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startService(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(++startId, new ComponentName(getPackageName(), JobHandlerService.class.getName())).setPersisted(true);
JobInfo.Builder builder = new JobInfo.Builder(JOB_SERVICE_ID, new ComponentName(getPackageName(), JobHandlerService.class.getName()))
.setPersisted(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//执行的最小延迟时间
builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);
Expand All @@ -42,23 +59,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
} else {
builder.setPeriodic(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS);
}
//省电模式下
if (KeepLive.sRunMode == KeepLive.RunMode.ENERGY) {
//需要网络
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
// 当插入充电器,执行该任务
builder.setRequiresCharging(true);
}
//需要网络
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
// 当插入充电器,执行该任务
builder.setRequiresCharging(true);
jobScheduler.schedule(builder.build());
}
return START_STICKY;
}

private void startService(Context context) {
if (!KeepLive.isKeepLive(context)) {
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (KeepLive.sForegroundNotification != null) {
Intent intent2 = new Intent(getApplicationContext(), NotificationClickReceiver.class);
Expand Down Expand Up @@ -90,8 +100,6 @@ public boolean onStopJob(JobParameters jobParameters) {
@Override
public void onDestroy() {
super.onDestroy();
if (!KeepLive.isKeepLive(this)) {
KeepLive.stopDoubleProcessService(this);
}
KeepLive.stopDoubleProcessService(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public IBinder onBind(Intent intent) {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!KeepLive.isKeepLive(this)) {
return super.onStartCommand(intent, flags, startId);
}

if (KeepLive.sUseSilenceMusic) {
//播放无声音乐
if (mMediaPlayer == null) {
Expand Down Expand Up @@ -162,10 +158,6 @@ private void pause() {
private class ScreenStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if (!KeepLive.isKeepLive(LocalService.this)) {
return;
}

String action = intent.getAction();
if (KEEP_ACTION_SCREEN_OFF.equals(action)) {
mIsPause = false;
Expand All @@ -189,10 +181,6 @@ public void wakeUp(String title, String description, int iconRes) throws RemoteE

@Override
public void onServiceDisconnected(ComponentName name) {
if (!KeepLive.isKeepLive(LocalService.this)) {
return;
}

if (ServiceUtils.isServiceRunning(getApplicationContext(), KEY_LOCAL_SERVICE_NAME)) {
Intent remoteService = new Intent(LocalService.this,
RemoteService.class);
Expand All @@ -211,10 +199,6 @@ public void onServiceDisconnected(ComponentName name) {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (!KeepLive.isKeepLive(LocalService.this)) {
return;
}

try {
if (mBinder != null && KeepLive.sForegroundNotification != null) {
GuardAidl guardAidl = GuardAidl.Stub.asInterface(service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.os.PowerManager;
import android.os.RemoteException;

import com.xuexiang.keeplive.KeepLive;
import com.xuexiang.keeplive.receiver.NotificationClickReceiver;
import com.xuexiang.keeplive.utils.NotificationUtils;
import com.xuexiang.keeplive.utils.ServiceUtils;
Expand Down Expand Up @@ -44,10 +43,6 @@ public IBinder onBind(Intent intent) {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!KeepLive.isKeepLive(this)) {
return super.onStartCommand(intent, flags, startId);
}

try {
mIsBoundLocalService = this.bindService(new Intent(RemoteService.this, LocalService.class), mConnection, Context.BIND_ABOVE_CLIENT);
} catch (Exception e) {
Expand All @@ -72,10 +67,6 @@ private final class GuardBinder extends GuardAidl.Stub {

@Override
public void wakeUp(String title, String description, int iconRes) throws RemoteException {
if (!KeepLive.isKeepLive(RemoteService.this)) {
return;
}

Intent intent = new Intent(getApplicationContext(), NotificationClickReceiver.class);
intent.setAction(NotificationClickReceiver.ACTION_CLICK_NOTIFICATION);
Notification notification = NotificationUtils.createNotification(RemoteService.this, title, description, iconRes, intent);
Expand All @@ -87,10 +78,6 @@ public void wakeUp(String title, String description, int iconRes) throws RemoteE
private final ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
if (!KeepLive.isKeepLive(RemoteService.this)) {
return;
}

if (ServiceUtils.isRunningTaskExist(getApplicationContext(), getPackageName() + ":remote")) {
Intent localService = new Intent(RemoteService.this, LocalService.class);
RemoteService.this.startService(localService);
Expand Down

0 comments on commit 4aea0e4

Please sign in to comment.