0
0
lihuiming 3 роки тому
батько
коміт
0fdd69100c

+ 17 - 4
app/src/main/AndroidManifest.xml

@@ -1,7 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
-    package="com.emato.ich" >
+    package="com.emato.ich"
+    android:sharedUserId="android.uid.system" >
 
 <!--    android:sharedUserId="android.uid.system"-->
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
@@ -15,11 +16,15 @@
     <!-- 自动更新需要的权限 -->
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.INTERNET"/>
-    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- android10+ 不能用 -->
     <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
-<!--    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />-->
-<!--    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />-->
+    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
+    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
+    <!--应用卸载权限-->
+    <uses-permission android:name="permission.REQUEST_INSTALL_PACKAGES" />
+    <uses-permission android:name="permission.REQUEST_DELETE_PACKAGES" />
+    <uses-permission android:name="android.permission.DELETE_PACKAGES" />
+
 
     <application
         android:name=".crash.CrashApplication"
@@ -57,6 +62,14 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </receiver>
+        <receiver
+            android:name="com.emato.ich.update.InstallResultReceiver"
+            android:enabled="true"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.content.pm.extra.STATUS"/>
+            </intent-filter>
+        </receiver>
 <!--        <receiver android:name=".update.InstallResultReceiver">-->
 <!--            <intent-filter>-->
 <!--                <action android:name="MyAction" />-->

+ 1 - 1
app/src/main/java/com/emato/ich/update/APKUpdateDownload.java

@@ -55,7 +55,7 @@ public class APKUpdateDownload {
                     @Override
                     public void onInstallSuccess() {
                         // TODO 重启应用 重启失败? 一直重启?
-//                        PackageManagerCompat.restartApp(context);
+                        PackageManagerCompat.restartApp(context);
                     }
                 });
             }

+ 1 - 1
app/src/main/java/com/emato/ich/update/InstallResultReceiver.java

@@ -16,7 +16,7 @@ public class InstallResultReceiver extends BroadcastReceiver {
             final int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS,PackageInstaller.STATUS_FAILURE);
             if (status == PackageInstaller.STATUS_SUCCESS) {
                 // TODO 安装成功 启动应用
-//                PackageManagerCompat.restartApp(context);
+                PackageManagerCompat.restartApp(context);
             } else {
                 // 失败
                 Log.e(TAG, "onReceive: 自动安装失败! ");

+ 14 - 14
app/src/main/java/com/emato/ich/update/PackageManagerCompat.java

@@ -148,18 +148,18 @@ public class PackageManagerCompat {
      * 重启
      * @param context   应用上下文
      */
-//    public static void restartApp(Context context) {
-//        Log.i(TAG, "restartApp: 应用程序重启中! 日期=====>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
-//        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
-//        PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
-//        AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
-//
-//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
-//            mgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, restartIntent);
-//
-//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
-//            mgr.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, restartIntent);
-//        }
-//        Log.i(TAG, "restartApp: 应用程序重启完成! 日期=====>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
-//    }
+    public static void restartApp(Context context) {
+        Log.i(TAG, "restartApp: 应用程序重启中! 日期=====>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
+        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
+        PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
+        AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
+            mgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, restartIntent);
+
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
+            mgr.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, restartIntent);
+        }
+        Log.i(TAG, "restartApp: 应用程序重启完成! 日期=====>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
+    }
 }