Преглед изворни кода

<发布v1.0.0版本用于云派8寸设备app升级自启动使用>

weizhengliang пре 3 година
родитељ
комит
607bb7ceb4

+ 18 - 3
app/build.gradle

@@ -3,19 +3,34 @@ plugins {
 }
 
 android {
-    compileSdkVersion 32
-    buildToolsVersion "32.0.0"
+    compileSdkVersion 30
+    buildToolsVersion "30.0.2"
 
     defaultConfig {
         applicationId "com.wd.app"
         minSdkVersion 23
-        targetSdkVersion 32
+        targetSdkVersion 30
         versionCode 1
         versionName "1.0"
 
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
     }
 
+    signingConfigs {
+        release {
+            storeFile file("keystore/keystore.jks")
+            storePassword "111111"
+            keyAlias "rk3128"
+            keyPassword "111111"
+        }
+        debug {
+            storeFile file("keystore/keystore.jks")
+            storePassword "111111"
+            keyAlias "rk3128"
+            keyPassword "111111"
+        }
+    }
+
     buildTypes {
         release {
             minifyEnabled false

BIN
app/keystore/keystore.jks


+ 18 - 0
app/release/output-metadata.json

@@ -0,0 +1,18 @@
+{
+  "version": 2,
+  "artifactType": {
+    "type": "APK",
+    "kind": "Directory"
+  },
+  "applicationId": "com.wd.app",
+  "variantName": "release",
+  "elements": [
+    {
+      "type": "SINGLE",
+      "filters": [],
+      "versionCode": 1,
+      "versionName": "1.0",
+      "outputFile": "app-release.apk"
+    }
+  ]
+}

BIN
app/release/yunpai_launch_app_1.0.apk


+ 8 - 1
app/src/main/AndroidManifest.xml

@@ -8,6 +8,13 @@
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
-        android:theme="@style/Theme.Yunpai_launch" />
+        android:theme="@style/Theme.Yunpai_launch" >
+
+        <service android:name=".MyService">
+            <intent-filter>
+                <action android:name="com.wd.MyService" />
+            </intent-filter>
+        </service>
+    </application>
 
 </manifest>

+ 139 - 0
app/src/main/java/com/wd/app/MyService.java

@@ -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();
+            }
+        }
+    }
+}