lib补充文件提交
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.fanjun.keeplive.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
|
||||
import com.fanjun.keeplive.KeepLive;
|
||||
import com.fanjun.keeplive.config.NotificationUtils;
|
||||
import com.fanjun.keeplive.receiver.NotificationClickReceiver;
|
||||
|
||||
/**
|
||||
* 隐藏前台服务通知
|
||||
*/
|
||||
public class HideForegroundService extends Service {
|
||||
private android.os.Handler handler;
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
startForeground();
|
||||
if (handler == null){
|
||||
handler = new Handler();
|
||||
}
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
}
|
||||
}, 2000);
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
|
||||
private void startForeground() {
|
||||
if (KeepLive.foregroundNotification != null) {
|
||||
Intent intent = new Intent(getApplicationContext(), NotificationClickReceiver.class);
|
||||
intent.setAction(NotificationClickReceiver.CLICK_NOTIFICATION);
|
||||
Notification notification = NotificationUtils.createNotification(this, KeepLive.foregroundNotification.getTitle(), KeepLive.foregroundNotification.getDescription(), KeepLive.foregroundNotification.getIconRes(), intent);
|
||||
startForeground(13691, notification);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// Source code recreated from a .class file by IntelliJ IDEA
|
||||
// (powered by Fernflower decompiler)
|
||||
//
|
||||
|
||||
package com.fanjun.keeplive.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.job.JobParameters;
|
||||
import android.app.job.JobScheduler;
|
||||
import android.app.job.JobService;
|
||||
import android.app.job.JobInfo.Builder;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build.VERSION;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.fanjun.keeplive.KeepLive;
|
||||
import com.fanjun.keeplive.config.NotificationUtils;
|
||||
import com.fanjun.keeplive.receiver.NotificationClickReceiver;
|
||||
import com.fanjun.keeplive.utils.ServiceUtils;
|
||||
|
||||
@RequiresApi(
|
||||
api = 21
|
||||
)
|
||||
public final class JobHandlerService extends JobService {
|
||||
private JobScheduler mJobScheduler;
|
||||
private int jobId = 100;
|
||||
|
||||
public JobHandlerService() {
|
||||
}
|
||||
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
this.startService(this);
|
||||
if (VERSION.SDK_INT >= 21) {
|
||||
this.mJobScheduler = (JobScheduler) this.getSystemService("jobscheduler");
|
||||
this.mJobScheduler.cancel(this.jobId);
|
||||
Builder builder = new Builder(this.jobId, new ComponentName(this.getPackageName(), JobHandlerService.class.getName()));
|
||||
if (VERSION.SDK_INT >= 24) {
|
||||
builder.setMinimumLatency(30000L);
|
||||
builder.setOverrideDeadline(30000L);
|
||||
builder.setMinimumLatency(30000L);
|
||||
builder.setBackoffCriteria(30000L, 0);
|
||||
} else {
|
||||
builder.setPeriodic(30000L);
|
||||
}
|
||||
|
||||
builder.setRequiredNetworkType(1);
|
||||
builder.setPersisted(true);
|
||||
this.mJobScheduler.schedule(builder.build());
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
private void startService(Context context) {
|
||||
Intent localIntent;
|
||||
if (VERSION.SDK_INT >= 26 && KeepLive.foregroundNotification != null) {
|
||||
localIntent = new Intent(this.getApplicationContext(), NotificationClickReceiver.class);
|
||||
localIntent.setAction("CLICK_NOTIFICATION");
|
||||
Notification notification = NotificationUtils.createNotification(this, KeepLive.foregroundNotification.getTitle(), KeepLive.foregroundNotification.getDescription(), KeepLive.foregroundNotification.getIconRes(), localIntent);
|
||||
this.startForeground(13691, notification);
|
||||
}
|
||||
|
||||
localIntent = new Intent(context, LocalService.class);
|
||||
Intent guardIntent = new Intent(context, RemoteService.class);
|
||||
this.startService(localIntent);
|
||||
this.startService(guardIntent);
|
||||
}
|
||||
|
||||
public boolean onStartJob(JobParameters jobParameters) {
|
||||
if (!ServiceUtils.isServiceRunning(this.getApplicationContext(), "com.fanjun.keeplive.service.LocalService") || !ServiceUtils.isRunningTaskExist(this.getApplicationContext(), this.getPackageName() + ":remote")) {
|
||||
this.startService(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onStopJob(JobParameters jobParameters) {
|
||||
if (!ServiceUtils.isServiceRunning(this.getApplicationContext(), "com.fanjun.keeplive.service.LocalService") || !ServiceUtils.isRunningTaskExist(this.getApplicationContext(), this.getPackageName() + ":remote")) {
|
||||
this.startService(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
package com.fanjun.keeplive.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.Service;
|
||||
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.media.MediaPlayer;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.os.RemoteException;
|
||||
|
||||
import com.fanjun.keeplive.KeepLive;
|
||||
import com.fanjun.keeplive.R;
|
||||
import com.fanjun.keeplive.config.NotificationUtils;
|
||||
import com.fanjun.keeplive.receiver.NotificationClickReceiver;
|
||||
import com.fanjun.keeplive.receiver.OnepxReceiver;
|
||||
import com.fanjun.keeplive.utils.ServiceUtils;
|
||||
|
||||
public final class LocalService extends Service {
|
||||
private OnepxReceiver mOnepxReceiver;
|
||||
private ScreenStateReceiver screenStateReceiver;
|
||||
private boolean isPause = true;//控制暂停
|
||||
private MediaPlayer mediaPlayer;
|
||||
private MyBilder mBilder;
|
||||
private android.os.Handler handler;
|
||||
private boolean mIsBoundRemoteService ;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
if (mBilder == null) {
|
||||
mBilder = new MyBilder();
|
||||
}
|
||||
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
|
||||
isPause = pm.isScreenOn();
|
||||
if (handler == null) {
|
||||
handler = new Handler();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return mBilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (KeepLive.useSilenceMusice){
|
||||
//播放无声音乐
|
||||
if (mediaPlayer == null) {
|
||||
mediaPlayer = MediaPlayer.create(this, R.raw.novioce);
|
||||
if (mediaPlayer!= null){
|
||||
mediaPlayer.setVolume(0f, 0f);
|
||||
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
||||
@Override
|
||||
public void onCompletion(MediaPlayer mediaPlayer) {
|
||||
if (!isPause) {
|
||||
if (KeepLive.runMode == KeepLive.RunMode.ROGUE) {
|
||||
play();
|
||||
} else {
|
||||
if (handler != null) {
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
play();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
|
||||
@Override
|
||||
public boolean onError(MediaPlayer mp, int what, int extra) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
play();
|
||||
}
|
||||
}
|
||||
}
|
||||
//像素保活
|
||||
if (mOnepxReceiver == null) {
|
||||
mOnepxReceiver = new OnepxReceiver();
|
||||
}
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction("android.intent.action.SCREEN_OFF");
|
||||
intentFilter.addAction("android.intent.action.SCREEN_ON");
|
||||
registerReceiver(mOnepxReceiver, intentFilter);
|
||||
//屏幕点亮状态监听,用于单独控制音乐播放
|
||||
if (screenStateReceiver == null) {
|
||||
screenStateReceiver = new ScreenStateReceiver();
|
||||
}
|
||||
IntentFilter intentFilter2 = new IntentFilter();
|
||||
intentFilter2.addAction("_ACTION_SCREEN_OFF");
|
||||
intentFilter2.addAction("_ACTION_SCREEN_ON");
|
||||
registerReceiver(screenStateReceiver, intentFilter2);
|
||||
//启用前台服务,提升优先级
|
||||
if (KeepLive.foregroundNotification != null) {
|
||||
Intent intent2 = new Intent(getApplicationContext(), NotificationClickReceiver.class);
|
||||
intent2.setAction(NotificationClickReceiver.CLICK_NOTIFICATION);
|
||||
Notification notification = NotificationUtils.createNotification(this, KeepLive.foregroundNotification.getTitle(), KeepLive.foregroundNotification.getDescription(), KeepLive.foregroundNotification.getIconRes(), intent2);
|
||||
startForeground(13691, notification);
|
||||
}
|
||||
//绑定守护进程
|
||||
try {
|
||||
Intent intent3 = new Intent(this, RemoteService.class);
|
||||
mIsBoundRemoteService = this.bindService(intent3, connection, Context.BIND_ABOVE_CLIENT);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
//隐藏服务通知
|
||||
try {
|
||||
if(Build.VERSION.SDK_INT < 25){
|
||||
startService(new Intent(this, HideForegroundService.class));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
if (KeepLive.keepLiveService != null) {
|
||||
KeepLive.keepLiveService.onWorking();
|
||||
}
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
private void play() {
|
||||
if (KeepLive.useSilenceMusice){
|
||||
if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
|
||||
mediaPlayer.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void pause() {
|
||||
if (KeepLive.useSilenceMusice){
|
||||
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
|
||||
mediaPlayer.pause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class ScreenStateReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(final Context context, Intent intent) {
|
||||
if (intent.getAction().equals("_ACTION_SCREEN_OFF")) {
|
||||
isPause = false;
|
||||
play();
|
||||
} else if (intent.getAction().equals("_ACTION_SCREEN_ON")) {
|
||||
isPause = true;
|
||||
pause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final class MyBilder extends GuardAidl.Stub {
|
||||
|
||||
@Override
|
||||
public void wakeUp(String title, String discription, int iconRes) throws RemoteException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceConnection connection = new ServiceConnection() {
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
if (ServiceUtils.isServiceRunning(getApplicationContext(), "com.fanjun.keeplive.service.LocalService")){
|
||||
Intent remoteService = new Intent(LocalService.this,
|
||||
RemoteService.class);
|
||||
LocalService.this.startService(remoteService);
|
||||
Intent intent = new Intent(LocalService.this, RemoteService.class);
|
||||
mIsBoundRemoteService = LocalService.this.bindService(intent, connection,
|
||||
Context.BIND_ABOVE_CLIENT);
|
||||
}
|
||||
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
|
||||
boolean isScreenOn = pm.isScreenOn();
|
||||
if (isScreenOn) {
|
||||
sendBroadcast(new Intent("_ACTION_SCREEN_ON"));
|
||||
} else {
|
||||
sendBroadcast(new Intent("_ACTION_SCREEN_OFF"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
try {
|
||||
if (mBilder != null && KeepLive.foregroundNotification != null) {
|
||||
GuardAidl guardAidl = GuardAidl.Stub.asInterface(service);
|
||||
guardAidl.wakeUp(KeepLive.foregroundNotification.getTitle(), KeepLive.foregroundNotification.getDescription(), KeepLive.foregroundNotification.getIconRes());
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (connection != null){
|
||||
try {
|
||||
if (mIsBoundRemoteService){
|
||||
unbindService(connection);
|
||||
}
|
||||
}catch (Exception e){}
|
||||
}
|
||||
try {
|
||||
unregisterReceiver(mOnepxReceiver);
|
||||
unregisterReceiver(screenStateReceiver);
|
||||
}catch (Exception e){}
|
||||
if (KeepLive.keepLiveService != null) {
|
||||
KeepLive.keepLiveService.onStop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.fanjun.keeplive.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.Service;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.os.RemoteException;
|
||||
|
||||
import com.fanjun.keeplive.config.NotificationUtils;
|
||||
import com.fanjun.keeplive.receiver.NotificationClickReceiver;
|
||||
import com.fanjun.keeplive.utils.ServiceUtils;
|
||||
|
||||
/**
|
||||
* 守护进程
|
||||
*/
|
||||
@SuppressWarnings(value={"unchecked", "deprecation"})
|
||||
public final class RemoteService extends Service {
|
||||
private MyBilder mBilder;
|
||||
private boolean mIsBoundLocalService ;
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
if (mBilder == null){
|
||||
mBilder = new MyBilder();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return mBilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
try {
|
||||
mIsBoundLocalService = this.bindService(new Intent(RemoteService.this, LocalService.class),
|
||||
connection, Context.BIND_ABOVE_CLIENT);
|
||||
}catch (Exception e){
|
||||
}
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (connection != null){
|
||||
try {
|
||||
if (mIsBoundLocalService){
|
||||
unbindService(connection);
|
||||
}
|
||||
}catch (Exception e){}
|
||||
}
|
||||
}
|
||||
private final class MyBilder extends GuardAidl.Stub {
|
||||
|
||||
@Override
|
||||
public void wakeUp(String title, String discription, int iconRes) throws RemoteException {
|
||||
if(Build.VERSION.SDK_INT < 25){
|
||||
Intent intent2 = new Intent(getApplicationContext(), NotificationClickReceiver.class);
|
||||
intent2.setAction(NotificationClickReceiver.CLICK_NOTIFICATION);
|
||||
Notification notification = NotificationUtils.createNotification(RemoteService.this, title, discription, iconRes, intent2);
|
||||
RemoteService.this.startForeground(13691, notification);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final ServiceConnection connection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
if (ServiceUtils.isRunningTaskExist(getApplicationContext(), getPackageName() + ":remote")){
|
||||
Intent localService = new Intent(RemoteService.this,
|
||||
LocalService.class);
|
||||
RemoteService.this.startService(localService);
|
||||
mIsBoundLocalService = RemoteService.this.bindService(new Intent(RemoteService.this,
|
||||
LocalService.class), connection, Context.BIND_ABOVE_CLIENT);
|
||||
}
|
||||
PowerManager pm = (PowerManager) RemoteService.this.getSystemService(Context.POWER_SERVICE);
|
||||
boolean isScreenOn = pm.isScreenOn();
|
||||
if (isScreenOn){
|
||||
sendBroadcast(new Intent("_ACTION_SCREEN_ON"));
|
||||
}else{
|
||||
sendBroadcast(new Intent("_ACTION_SCREEN_OFF"));
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user