android_job_scheduler 0.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 14

android_job_scheduler_example #

Schedule Jobs using Android's JobScheduler API. This is very much Work in progress. Much of this Plugin is based on the android_alarm_manager Plugin.

Getting Started #

Add the Dependency #

tbd

Declare the JobScheduler Service in AndroidManifest.xml

In your project's android/app/src/main directory, open the AndroidManifest.xml and add the following to the <application /> block:

<service
    android:exported="true"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:name="io.gjg.androidjobscheduler.AndroidJobScheduler">

Usage #

import 'package:android_job_scheduler/android_job_scheduler.dart';
// This MUST be a top level Function or a Static Class Member. It may not be a Class Method
// or a Closure. Inside this method, you don't have access to anything assuming a running
// Application.
void iRunPeriodically() {
    print('This gets run periodically by the Android JobScheduler API. '
          'Even when the App is not running. '
          'The Timings are not guaranteed to be exact by the Android OS, though.');
}

void main() {
    ...
    bool jobIsInstalled =
        await AndroidJobScheduler.scheduleEvery(
            const Duration(seconds: 10), iRunPeriodically);
}

For help getting started with Flutter, view our online documentation.

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

example/lib/main.dart

import 'dart:io';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:android_job_scheduler/android_job_scheduler.dart';

import 'package:path_provider/path_provider.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

void yolo() async {
  // Search Logcat for Yolo to see the the Callback firing
  // when the app is not running.
  print('Yolo executing');
  final file = await getCommonStateFile();
  if (!await file.exists()) {
    await file.writeAsString("0");
  } else {
    final contents = await file.readAsString();
    print("Contents: $contents");
    final timesCalled = int.parse(contents);
    await file.writeAsString("${timesCalled + 1}");
  }
}

Future<File> getCommonStateFile() async {
  final targetDir = await getApplicationDocumentsDirectory();
  return new File("${targetDir.path}/times_called.txt");
}

class _MyAppState extends State<MyApp> {
  bool _jobIsInstalled;
  int _timesCalled = 0;

  @override
  initState() {
    super.initState();
    initFileWatcher();
    updateCallBackTimesCalled();
  }

  initFileWatcher() async {
    Timer.periodic(const Duration(seconds: 5), updateCallBackTimesCalled);
  }

  updateCallBackTimesCalled([Timer _]) async {
    final file = await getCommonStateFile();
    var timesCalled;
    if (!await file.exists()) {
      timesCalled = 0;
    } else {
      timesCalled = int.parse(await file.readAsString());
    }
    setState(() {
      _timesCalled = timesCalled;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
          appBar: new AppBar(
            title: new Text('Android Job Scheduler'),
          ),
          body: new Center(
              child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Text(_jobIsInstalled == null? 'Job not installed.' : _jobIsInstalled? 'Job is installed!' : 'Job installation failed!'),
              new Text('Callback has been called '),
              new Text('$_timesCalled', textScaleFactor: 2.0),
              new Text('times.'),
              new Divider(),
              new Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new RaisedButton.icon(
                        onPressed: () async {
                          bool jobIsInstalled;
                          try {
                            jobIsInstalled =
                                await AndroidJobScheduler.scheduleEvery(
                                    const Duration(seconds: 10), yolo);
                          } finally {
                            setState(() {
                              _jobIsInstalled = jobIsInstalled == true;
                            });
                          }
                        },
                        icon: const Icon(Icons.check_box),
                        label: Text('Install Job')),
                    new Container(width: 10.0,),
                    new RaisedButton.icon(
                        onPressed: () {},
                        icon: const Icon(Icons.delete),
                        label: Text('Uninstall Job')),
                  ]),
              new Divider(),
              new RaisedButton.icon(onPressed: () async {
                final file = await getCommonStateFile();
                if (await file.exists()) {
                  await file.delete();
                  await updateCallBackTimesCalled();
                }
              }, icon: const Icon(Icons.fast_rewind), label: const Text('Reset State')),
              new Container(
                padding: EdgeInsets.all(20.0),
                child: const Text('Close the App, wait a few secs, and see what happens!'),
              )
              
            ],
          ))),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  android_job_scheduler: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:android_job_scheduler/android_job_scheduler.dart';
  
Version Uploaded Documentation Archive
0.0.7 Jun 13, 2018 Go to the documentation of android_job_scheduler 0.0.7 Download android_job_scheduler 0.0.7 archive
0.0.6 Jun 13, 2018 Go to the documentation of android_job_scheduler 0.0.6 Download android_job_scheduler 0.0.6 archive
0.0.5 Jun 12, 2018 Go to the documentation of android_job_scheduler 0.0.5 Download android_job_scheduler 0.0.5 archive
0.0.4 Jun 12, 2018 Go to the documentation of android_job_scheduler 0.0.4 Download android_job_scheduler 0.0.4 archive
0.0.3 Jun 12, 2018 Go to the documentation of android_job_scheduler 0.0.3 Download android_job_scheduler 0.0.3 archive
0.0.2 Jun 12, 2018 Go to the documentation of android_job_scheduler 0.0.2 Download android_job_scheduler 0.0.2 archive
0.0.1 Jun 11, 2018 Go to the documentation of android_job_scheduler 0.0.1 Download android_job_scheduler 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
29
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
14
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <2.0.0