init
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.info.sn.service;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
public class APKinstallReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// TODO: This method is called when the BroadcastReceiver is receiving
|
||||
// an Intent broadcast.
|
||||
|
||||
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED) || intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
|
||||
String packageName = intent.getDataString().replace("package:", "");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
23
app/src/main/java/com/info/sn/service/BootReceiver.java
Normal file
23
app/src/main/java/com/info/sn/service/BootReceiver.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.info.sn.service;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
public class BootReceiver extends BroadcastReceiver {
|
||||
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.e("BootReceiver", intent.getAction());
|
||||
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
|
||||
// Intent i = new Intent(context, InitJpushServer.class);
|
||||
// context.startService(i);
|
||||
context.startService(new Intent(context, StepService.class));
|
||||
context.startService(new Intent(context, GuardService.class));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
67
app/src/main/java/com/info/sn/service/GuardService.java
Normal file
67
app/src/main/java/com/info/sn/service/GuardService.java
Normal file
@@ -0,0 +1,67 @@
|
||||
package com.info.sn.service;
|
||||
|
||||
/**
|
||||
* 作者 mjsheng
|
||||
* 日期 2019/4/1 10:58
|
||||
* 邮箱 501802639@qq.com
|
||||
* 来自:
|
||||
*/
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.IBinder;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.info.sn.KeepAliveConnection;
|
||||
import com.info.sn.utils.LogUtils;
|
||||
import com.info.sn.utils.ServiceAliveUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 守护进程 双进程通讯
|
||||
*
|
||||
* @author LiGuangMin
|
||||
* @time Created by 2018/8/17 11:27
|
||||
*/
|
||||
public class GuardService extends Service {
|
||||
private final static String TAG = GuardService.class.getSimpleName();
|
||||
private ServiceConnection mServiceConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
||||
LogUtils.e(TAG, "GuardService:建立链接");
|
||||
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
|
||||
if (!isServiceRunning) {
|
||||
Intent i = new Intent(GuardService.this, MyDownloadService.class);
|
||||
startService(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName componentName) {
|
||||
// 断开链接
|
||||
startService(new Intent(GuardService.this, StepService.class));
|
||||
// 重新绑定
|
||||
bindService(new Intent(GuardService.this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return new KeepAliveConnection.Stub() {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
// 绑定建立链接
|
||||
bindService(new Intent(this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
159
app/src/main/java/com/info/sn/service/MyDownloadService.java
Normal file
159
app/src/main/java/com/info/sn/service/MyDownloadService.java
Normal file
@@ -0,0 +1,159 @@
|
||||
package com.info.sn.service;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Service;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.info.sn.BuildConfig;
|
||||
import com.info.sn.MyApplication;
|
||||
import com.info.sn.R;
|
||||
import com.info.sn.network.api.HTTPInterface;
|
||||
import com.info.sn.utils.ApkUtils;
|
||||
import com.info.sn.utils.LogUtils;
|
||||
import com.lzy.okgo.OkGo;
|
||||
import com.lzy.okgo.callback.FileCallback;
|
||||
import com.lzy.okgo.model.Progress;
|
||||
import com.lzy.okgo.model.Response;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
// 下载管理服务
|
||||
public class MyDownloadService extends Service {
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
flags = START_STICKY;
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
synchronized private void CheckUpdate() {
|
||||
HTTPInterface.checkUpdateByPackage(handler, this.getPackageName(), String.valueOf(BuildConfig.VERSION_CODE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
startService(new Intent(this, StepService.class));
|
||||
startService(new Intent(this, GuardService.class));
|
||||
CheckUpdate();
|
||||
// new Thread(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// while (true) {
|
||||
// LogUtils.e("packagename", MyApplication.getTaskPackname());
|
||||
// LogUtils.e("packagename", String.valueOf(MyApplication.getSystemBattery()));
|
||||
//// MyApplication.sendStartTime();
|
||||
// try {
|
||||
// Thread.sleep(5000);
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }).start();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Handler handler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(@NonNull Message msg) {
|
||||
super.handleMessage(msg);
|
||||
switch (msg.what) {
|
||||
case 0:
|
||||
|
||||
break;
|
||||
case 200:
|
||||
Bundle bundle = (Bundle) msg.obj;
|
||||
getFile(bundle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private void getFile(final Bundle bundle) {
|
||||
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
|
||||
path.mkdirs();
|
||||
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/Update" + bundle.getString("versionCode") + ".apk");
|
||||
|
||||
if (file.exists() && file.isFile()) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this)
|
||||
.setTitle("软件更新")
|
||||
.setIcon(R.mipmap.ic_launcher_home)
|
||||
.setCancelable(false)
|
||||
.setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
|
||||
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialogInterface, int i) {
|
||||
ApkUtils.installApk(MyDownloadService.this, file);
|
||||
dialogInterface.dismiss();
|
||||
}
|
||||
});
|
||||
AlertDialog ad = builder.create();
|
||||
ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
||||
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
|
||||
ad.show();
|
||||
} else {
|
||||
OkGo.<File>get(bundle.getString("url"))
|
||||
.execute(new FileCallback("Sninfo/Update" + bundle.getString("versionCode") + ".apk") {
|
||||
@Override
|
||||
public void onSuccess(final Response<File> response) {
|
||||
// Settings.System.putString(getApplicationContext().getContentResolver(), "qch_app_forbid", "com.baidu.video");
|
||||
// ApkUtils.installApkInSilence(response.body().getAbsolutePath(), Launcher.this.getPackageName());
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(MyDownloadService.this)
|
||||
.setTitle("软件更新")
|
||||
.setIcon(R.mipmap.ic_launcher_home)
|
||||
.setCancelable(false)
|
||||
.setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
|
||||
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialogInterface, int i) {
|
||||
ApkUtils.installApk(MyDownloadService.this, response.body());
|
||||
dialogInterface.dismiss();
|
||||
}
|
||||
});
|
||||
AlertDialog ad = builder.create();
|
||||
ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
||||
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
|
||||
ad.show();
|
||||
Log.e("getFile", "download file successful,now installing");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Response<File> response) {
|
||||
super.onError(response);
|
||||
Log.e("getFile", "File download Failure" + response.getException());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadProgress(Progress progress) {
|
||||
super.downloadProgress(progress);
|
||||
Log.e("getFile", "已下载:" + progress.currentSize + ",总大小:" + progress.totalSize + ",进度:" + progress.fraction + ",当前网速:" + progress.speed);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
68
app/src/main/java/com/info/sn/service/StepService.java
Normal file
68
app/src/main/java/com/info/sn/service/StepService.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.info.sn.service;
|
||||
|
||||
/**
|
||||
* 作者 mjsheng
|
||||
* 日期 2019/4/1 10:57
|
||||
* 邮箱 501802639@qq.com
|
||||
* 来自:
|
||||
*/
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.IBinder;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.info.sn.KeepAliveConnection;
|
||||
import com.info.sn.utils.LogUtils;
|
||||
import com.info.sn.utils.ServiceAliveUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 主进程 双进程通讯
|
||||
*
|
||||
* @author LiGuangMin
|
||||
* @time Created by 2018/8/17 11:26
|
||||
*/
|
||||
public class StepService extends Service {
|
||||
|
||||
private final static String TAG = StepService.class.getSimpleName();
|
||||
private ServiceConnection mServiceConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
||||
LogUtils.e(TAG, "StepService:建立链接");
|
||||
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice();
|
||||
if (!isServiceRunning) {
|
||||
Intent i = new Intent(StepService.this, MyDownloadService.class);
|
||||
startService(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName componentName) {
|
||||
// 断开链接
|
||||
startService(new Intent(StepService.this, GuardService.class));
|
||||
// 重新绑定
|
||||
bindService(new Intent(StepService.this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return new KeepAliveConnection.Stub() {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
// startForeground(1, new Notification());
|
||||
// 绑定建立链接
|
||||
bindService(new Intent(this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user