|
@@ -0,0 +1,139 @@
|
|
|
+package com.wd.app;
|
|
|
+
|
|
|
+import android.app.Notification;
|
|
|
+import android.app.NotificationChannel;
|
|
|
+import android.app.NotificationManager;
|
|
|
+import android.app.Service;
|
|
|
+import android.content.ActivityNotFoundException;
|
|
|
+import android.content.BroadcastReceiver;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.IntentFilter;
|
|
|
+import android.content.pm.PackageInfo;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.os.Binder;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.IBinder;
|
|
|
+import android.support.annotation.Nullable;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class MyService extends Service {
|
|
|
+ private String TAG = "WdMyService";
|
|
|
+ Binder myBinder = new Binder();
|
|
|
+
|
|
|
+ private NotificationManager notificationManager = null;
|
|
|
+ private String notificationId = "channelId";
|
|
|
+ private String notificationName = "channelName";
|
|
|
+
|
|
|
+ private InstallResultReceiver installReceiver = null;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate() {
|
|
|
+ super.onCreate();
|
|
|
+
|
|
|
+ notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ NotificationChannel channel = new NotificationChannel(
|
|
|
+ notificationId,
|
|
|
+ notificationName,
|
|
|
+ NotificationManager.IMPORTANCE_HIGH
|
|
|
+ );
|
|
|
+ notificationManager.createNotificationChannel(channel);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ IntentFilter intentFilter = new IntentFilter();
|
|
|
+ intentFilter.addAction("com.miki.slient.INSTALL_CUSTOMER_PACKAGES_RESULT");
|
|
|
+ installReceiver = new InstallResultReceiver();
|
|
|
+ registerReceiver(installReceiver, intentFilter);
|
|
|
+
|
|
|
+ Log.d(TAG, "service已启动");
|
|
|
+ }
|
|
|
+
|
|
|
+ private Notification getNotification() {
|
|
|
+ Notification.Builder builder = new Notification.Builder(this)
|
|
|
+ .setSmallIcon(R.mipmap.ic_launcher)
|
|
|
+ .setContentTitle("app服务")
|
|
|
+ .setContentText("系统服务已启动")
|
|
|
+ .setOnlyAlertOnce(true);
|
|
|
+ //设置Notification的ChannelID,否则不能正常显示
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ builder.setChannelId(notificationId);
|
|
|
+ }
|
|
|
+ Notification notification = builder.build();
|
|
|
+ return notification;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ startForeground(110, getNotification()); //开前台服务
|
|
|
+ }
|
|
|
+ //return START_STICKY//当服务被异常终止时,重启服务
|
|
|
+ return super.onStartCommand(intent, flags, startId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nullable
|
|
|
+ @Override
|
|
|
+ public IBinder onBind(Intent intent) {
|
|
|
+ return myBinder;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ stopForeground(true); // 停止前台服务--参数:表示是否移除之前的通知
|
|
|
+ }
|
|
|
+ if (installReceiver != null) {
|
|
|
+ unregisterReceiver(installReceiver);
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d(TAG, "服务停止");
|
|
|
+ }
|
|
|
+
|
|
|
+ //启动wdkl app
|
|
|
+ private void startMyApp() {
|
|
|
+ try {
|
|
|
+ List<PackageInfo> packageInfos = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES |
|
|
|
+ PackageManager.GET_SERVICES);
|
|
|
+ for (PackageInfo info : packageInfos) {
|
|
|
+ String pkg = info.packageName;
|
|
|
+ if (pkg.contains("com.wdkl")) {
|
|
|
+ try {
|
|
|
+ Intent target = getPackageManager().getLaunchIntentForPackage(pkg);
|
|
|
+ target.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ startActivity(target);
|
|
|
+ } catch (ActivityNotFoundException e) {
|
|
|
+ Log.d(TAG, "ActivityNotFoundException");
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.d(TAG, "Exception");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Throwable t) {
|
|
|
+ t.printStackTrace();;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class InstallResultReceiver extends BroadcastReceiver {
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ if("com.miki.slient.INSTALL_CUSTOMER_PACKAGES_RESULT".equals(intent.getAction())) {
|
|
|
+ boolean result = intent.getBooleanExtra("result",false);
|
|
|
+ if(result) {
|
|
|
+ Log.d(TAG, "升级成功");
|
|
|
+ }else{
|
|
|
+ Log.d(TAG, "升级失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ //不管有没有升级成功都重启
|
|
|
+ startMyApp();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|