From 0efc656c95ed392a175ea7feb70b94416363096e Mon Sep 17 00:00:00 2001 From: rhyme Date: Mon, 6 Dec 2021 12:17:55 +0800 Subject: [PATCH] =?UTF-8?q?=20fixed=20android=20compile=20SDK=20version=20?= =?UTF-8?q?=3D=2031=20crash=20error,upgrade=20version=20click=20cancel=20c?= =?UTF-8?q?rash=20error=E3=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ .../r_upgrade/common/UpgradeNotification.java | 23 +++++++++++++++---- .../r_upgrade/common/UpgradeService.java | 6 +++-- example/android/app/build.gradle | 4 ++-- .../android/app/src/main/AndroidManifest.xml | 1 + example/lib/main.dart | 2 +- example/pubspec.lock | 10 ++++---- pubspec.lock | 8 +++---- pubspec.yaml | 2 +- 9 files changed, 39 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa18017..6762058 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## 0.3.7 +* fixed android compile SDK version = 31 crash error,upgrade version click cancel crash error、 ## 0.3.6 * add android platform [androidStores]、[getVersionFromAndroidStore] methods. ## 0.3.5 diff --git a/android/src/main/java/com/example/r_upgrade/common/UpgradeNotification.java b/android/src/main/java/com/example/r_upgrade/common/UpgradeNotification.java index 5cada3e..5d40d38 100644 --- a/android/src/main/java/com/example/r_upgrade/common/UpgradeNotification.java +++ b/android/src/main/java/com/example/r_upgrade/common/UpgradeNotification.java @@ -38,8 +38,11 @@ static void createNotification(Context context, int id, String title, int curren pauseIntent.setAction(UpgradeService.RECEIVER_PAUSE); pauseIntent.putExtra(UpgradeManager.PARAMS_ID, id); pauseIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName()); + + PendingIntent pausePendingIntent = - PendingIntent.getBroadcast(context, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent.getBroadcast(context, 0, pauseIntent, getPendingIntentFlag()); +// PendingIntent.getBroadcast(context, 0, pauseIntent, PendingIntent.FLAG_IMMUTABLE); boolean indeterminate = max_length == -1; notification = new NotificationCompat.Builder(context, CHANNEL_NAME) @@ -56,7 +59,8 @@ static void createNotification(Context context, int id, String title, int curren installIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName()); PendingIntent installPendingIntent = - PendingIntent.getBroadcast(context, 0, installIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent.getBroadcast(context, 0, installIntent,getPendingIntentFlag()); +// PendingIntent.getBroadcast(context, 0, installIntent, PendingIntent.FLAG_IMMUTABLE); notification = new NotificationCompat.Builder(context, CHANNEL_NAME) .setSmallIcon(context.getApplicationInfo().icon) @@ -71,7 +75,9 @@ static void createNotification(Context context, int id, String title, int curren reStartIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName()); PendingIntent reStartPendingIntent = - PendingIntent.getBroadcast(context, 0, reStartIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent.getBroadcast(context, 0, reStartIntent, getPendingIntentFlag()); +// PendingIntent.getBroadcast(context, 0, reStartIntent, PendingIntent.FLAG_IMMUTABLE); + notification = new NotificationCompat.Builder(context, CHANNEL_NAME) .setSmallIcon(context.getApplicationInfo().icon) .setContentTitle(title) @@ -85,7 +91,8 @@ static void createNotification(Context context, int id, String title, int curren failedIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName()); PendingIntent reStartPendingIntent = - PendingIntent.getBroadcast(context, 0, failedIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent.getBroadcast(context, 0, failedIntent, getPendingIntentFlag()); +// PendingIntent.getBroadcast(context, 0, failedIntent, PendingIntent.FLAG_IMMUTABLE); notification = new NotificationCompat.Builder(context, CHANNEL_NAME) .setSmallIcon(context.getApplicationInfo().icon) .setContentTitle(title) @@ -109,6 +116,14 @@ static void createNotification(Context context, int id, String title, int curren // notificationManager.notify(id, notification); } + private static int getPendingIntentFlag() { + int pendingFlag = PendingIntent.FLAG_UPDATE_CURRENT; + if (Build.VERSION.SDK_INT >= 31) { + pendingFlag = PendingIntent.FLAG_IMMUTABLE; + } + return pendingFlag; + } + static void removeNotification(Context context, long id) { NotificationManagerCompat compat = NotificationManagerCompat.from(context); compat.cancel((int) id); diff --git a/android/src/main/java/com/example/r_upgrade/common/UpgradeService.java b/android/src/main/java/com/example/r_upgrade/common/UpgradeService.java index 6ad6f03..0517b69 100644 --- a/android/src/main/java/com/example/r_upgrade/common/UpgradeService.java +++ b/android/src/main/java/com/example/r_upgrade/common/UpgradeService.java @@ -160,7 +160,7 @@ private void handleNetworkChange(boolean isConnected) { @Override public int onStartCommand(Intent intent, int flags, int startId) { - + if (intent == null) return super.onStartCommand(null, flags, startId); Bundle bundle = intent.getExtras(); assert (bundle != null); String url = bundle.getString(DOWNLOAD_URL); @@ -190,7 +190,9 @@ public int onStartCommand(Intent intent, int flags, int startId) { @Override public void onDestroy() { unregisterReceiver(actionReceiver); - runnable.handlerDownloadPause(); + if (runnable != null) { + runnable.handlerDownloadPause(); + } super.onDestroy(); } diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 892aaa5..f59f17e 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 28 + compileSdkVersion 31 sourceSets { main.java.srcDirs += 'src/main/kotlin' @@ -40,7 +40,7 @@ android { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.r_upgrade_example" minSdkVersion 16 - targetSdkVersion 28 + targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 54f2dca..5d6a922 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -15,6 +15,7 @@ android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" + android:exported="true" android:windowSoftInputMode="adjustResize">