Skip to content

Commit

Permalink
build(android_alarm_manager_plus): Update to target and compile SDK 3…
Browse files Browse the repository at this point in the history
…4 on Android (#2714)
  • Loading branch information
vbuberen committed Mar 17, 2024
1 parent 641e790 commit 0262766
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 39 deletions.
6 changes: 3 additions & 3 deletions packages/android_alarm_manager_plus/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 33
compileSdk 34

namespace 'dev.fluttercommunity.plus.androidalarmmanager'

Expand All @@ -39,7 +39,7 @@ android {
}

defaultConfig {
minSdkVersion 19
minSdk 19
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand All @@ -51,5 +51,5 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.6.1'
api 'androidx.core:core:1.10.1'
api 'androidx.core:core-ktx:1.12.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 33
compileSdk 34

namespace 'com.example.example'

Expand All @@ -35,6 +35,10 @@ android {
targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = 17
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
Expand All @@ -46,8 +50,8 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.example"
minSdkVersion 21
targetSdkVersion 31
minSdk 21
targetSdk 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
Expand Down
92 changes: 62 additions & 30 deletions packages/android_alarm_manager_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:math';
import 'dart:ui';

import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -66,17 +67,26 @@ class _AlarmHomePage extends StatefulWidget {

class _AlarmHomePageState extends State<_AlarmHomePage> {
int _counter = 0;
PermissionStatus _exactAlarmPermissionStatus = PermissionStatus.granted;

@override
void initState() {
super.initState();
AndroidAlarmManager.initialize();
_checkExactAlarmPermission();

// Register for events from the background isolate. These messages will
// always coincide with an alarm firing.
port.listen((_) async => await _incrementCounter());
}

void _checkExactAlarmPermission() async {
final currentStatus = await Permission.scheduleExactAlarm.status;
setState(() {
_exactAlarmPermissionStatus = currentStatus;
});
}

Future<void> _incrementCounter() async {
developer.log('Increment counter!');
// Ensure we've loaded the updated count from the background isolate.
Expand Down Expand Up @@ -114,43 +124,65 @@ class _AlarmHomePageState extends State<_AlarmHomePage> {
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
children: [
const Spacer(),
Text(
'Alarms fired during this run of the app: $_counter',
style: textStyle,
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
Text(
'Alarm fired $_counter times',
'Total alarms fired since app installation: ${prefs?.getInt(countKey).toString() ?? ''}',
style: textStyle,
textAlign: TextAlign.center,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Total alarms fired: ',
style: textStyle,
),
Text(
prefs?.getInt(countKey).toString() ?? '',
key: const ValueKey('BackgroundCountText'),
style: textStyle,
),
],
const Spacer(),
if (_exactAlarmPermissionStatus.isDenied)
Text(
'SCHEDULE_EXACT_ALARM is denied\n\nAlarms scheduling is not available',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleMedium,
)
else
Text(
'SCHEDULE_EXACT_ALARM is granted\n\nAlarms scheduling is available',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 32),
ElevatedButton(
onPressed: _exactAlarmPermissionStatus.isDenied
? () async {
await Permission.scheduleExactAlarm
.onGrantedCallback(() => setState(() {
_exactAlarmPermissionStatus =
PermissionStatus.granted;
}))
.request();
}
: null,
child: const Text('Request exact alarm permission'),
),
const SizedBox(height: 24),
const SizedBox(height: 32),
ElevatedButton(
key: const ValueKey('RegisterOneShotAlarm'),
onPressed: () async {
await AndroidAlarmManager.oneShot(
const Duration(seconds: 5),
// Ensure we have a unique alarm ID.
Random().nextInt(pow(2, 31) as int),
callback,
exact: true,
wakeup: true,
);
},
child: const Text(
'Schedule OneShot Alarm',
),
onPressed: _exactAlarmPermissionStatus.isGranted
? () async {
await AndroidAlarmManager.oneShot(
const Duration(seconds: 5),
// Ensure we have a unique alarm ID.
Random().nextInt(pow(2, 31) as int),
callback,
exact: true,
wakeup: true,
);
}
: null,
child: const Text('Schedule OneShot Alarm'),
),
const Spacer(),
],
),
),
Expand Down
6 changes: 3 additions & 3 deletions packages/android_alarm_manager_plus/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ dependencies:
flutter:
sdk: flutter
android_alarm_manager_plus: ^3.0.4
shared_preferences: ^2.1.0
path_provider: ^2.0.14
permission_handler: ^11.3.0
shared_preferences: ^2.2.2

dev_dependencies:
espresso: ^0.3.0+4
espresso: ^0.3.0+7
flutter_test:
sdk: flutter
integration_test:
Expand Down

0 comments on commit 0262766

Please sign in to comment.