Giter VIP home page Giter VIP logo

trigger's Introduction

Android Arsenal

What is Trigger

Do you know JobScheduler which is new added in Android Lollipop version? It is a job scheduler as its name. Define your own job, which need some conditions to happen, suit it up with such as device charging or unmeterred network(just like wifi, what else we have?) or device idle status, then throw it to the JobScheduler framework, your job will happen while all these conditions are satisfied. Awesome feature, isn't it? But a pity is it's only available above API 21. So I try my best to copy it to API 14 and named it Trigger.

Can Trigger do what JobScheduler do?

Good question! In my opinion and according to the test result, it do CAN, and I think Trigger can do more.

Features

  • support multi conditions combination with one job
  • inner conditions: device charging, unmeterred network and idle status
  • support persist job, means that persist-able job can be triggered after device reboot
  • support job's deadline, last chance to be triggered
  • allow your job's action runs in background or main thread, follow your configuration

While all your job's conditions are satisfied, your job's action just like a Duang~~~ Happened!

duang

How does Trigger work?

There is a Android service component running in background named TriggerLoop, its duty is managing all jobs you scheduled, updating their status, checking them if they can be triggered, handling remove request from user, and even recovering them from service restarting or device rebooting. Do not forget declare it in your AndroidManifest.xml, otherwise Trigger can not start at all.

Usage

Support Api Level

Android ICS (API14)

Include in your project

  • Maven

    <dependency>
        <groupId>com.github.airk000</groupId>
        <artifactId>trigger</artifactId>
        <version>1.1.0</version>
    </dependency>
    
  • Gradle

    compile 'com.github.airk000:trigger:1.1.1'
    

Declare

You should add these code in your application's AndroidManifest.xml, first is permissions:

<!-- inner status controller: network controller will need it -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- wakelock helps Trigger work well, if you really don't like it, remove it, doesn't matter -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- that's the point why Trigger can recover persist-able job after device rebooting, if you don't need it, never mind -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

then components:

<service android:name="com.github.airk.trigger.TriggerLoop" />
<!-- as what I said in permission, if you don't need it, remove it -->
<receiver android:name="com.github.airk.trigger.PersistReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Schedule a Job

All operations should through Trigger instance, you can get it by using following code:

Trigger trigger = Trigger.getInstance(context);

It is in singleton design, so you will always get the same instance wherever invoke this.

Then new a simple and un-persist-able job and schedule it:

Job job = new Job(new Action() {
        @Override
        protected void act() {
            //do something
        }
    }).withExtra(new Condition() {
        @Override
        public String[] getAction() {
            return new String[]{YOUR_BROARCAST};
        }
    });
trigger.schedule(job);

After all these, while you sending YOUR_BROADCAST broadcast wherever you want, the job's action(//do something... there) will be triggered in background thread (want main thread? see later).

More configurations

  • multi constructors can give you chances to give your job a tag, or mark it want be persist
  • different from Action class, ContextAction give you a chance to do something with a context (it is the TriggerLoop)
  • withExtra let you add more conditions to your job
  • repeat make the job can be triggered repeatability, and repeat(long ms) version give it a chance to not trigger too often
  • deadline setup the deadline of the job, it's in RTC
  • attachOn MAIN and BACKGROUND, MAIN means the job's action will be triggered in main thread, default is BACKGROUND

Inner conditions

  • networkType limit your job can be triggered in which network environment
  • needCharging limit your job can only be triggered while device is in charing status
  • needDeviceIdle limit your job can only be triggered while device is idle

PS: How to define the idle status? After your device drop into daydreaming or just screen off 71 minutes later.

About Job PERSIST

If you want your job can be persist after reboot, you really need use PUBLIC and STATIC Action class and same modifier Condition.

Tips

Trigger will create two directory in your application's data space, named job_backup and job_persist. job_backup keeps the jobs can be recovered after service restarting, so even though this service has been killed by some reasons, jobs here can be put into the waiting list also, and of course, only persist-able jobs can be saved here. job_persist keeps the jobs can be recovered after device rebooting, if you wan let your job do this, use the constructors which have persistAfterReboot parameter to construct your job instance.

Last

If you like this project, STAR it!

If your application is using this and already online, please let me know through E-mail.

If you have any question, open an issue.

If you have any good idea to improve this project or fixed some bugs, give me a Pull-request, I'm happy to build this project better with your help.

License

Copyright 2015 Kevin Liu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

trigger's People

Contributors

lkv1988 avatar bryant1410 avatar

Stargazers

 avatar tim_qiangj avatar BHawk  avatar Javad Ghane  avatar xiaopeng avatar Zulqurnain Haider avatar Sotiris Falieris avatar lin avatar Arslan Maqbool avatar  avatar  avatar  avatar  avatar AmorのGemini avatar  avatar wkfym avatar wangshenglongrc avatar  avatar Florian avatar Matteo Lobello avatar 行一 avatar  avatar Louis CAD avatar  avatar Rajkumar Magar avatar Kin Kaku avatar Himanshu Singh avatar  avatar E-KAY avatar Anderson大码渣 avatar  avatar liandaochuhai avatar  avatar flyfire avatar philos3 avatar NaughtyDandan avatar Jiandong Wang avatar 김현준 avatar  avatar 毛昌越 avatar zhsheng avatar Omar altamimi avatar What avatar Merlin avatar Hymane avatar hydcoder avatar 蔡健烽 avatar 巴掌 avatar 顿顿顿顿文 avatar ly_one avatar QuincySx avatar Byungjik Roh avatar charming avatar WilliamChik avatar chan avatar Fishand avatar  avatar Aaron avatar  avatar Yolanda Dewi avatar Jack Wang avatar Momo (Edda) Lab avatar  avatar  avatar alfongj avatar  avatar zjy avatar Micro·J avatar Tangxb avatar  avatar Angel Freire avatar Matías Agustín Méndez avatar Adrián Mouly avatar Sandeep Reddy avatar Mariusz Krok avatar  avatar dawson avatar  avatar Sebastian Kaspari avatar Wendy avatar Joe Munoz avatar Zaicheng Qi avatar  avatar cppgohan avatar Tome Okin avatar Amanda Riu avatar  avatar longtaoger avatar  avatar s7 avatar Renan Ferrari avatar Hasan Abuzayed avatar twiceYuan avatar  avatar Andy avatar  avatar  avatar James avatar 胡本 avatar xiong avatar

Watchers

 avatar  avatar Dennis Drzosga avatar  avatar  avatar  avatar jhyc avatar bullet avatar  avatar Ren  Yu avatar  avatar David avatar Lawrence C. Cendaña avatar tiny Flutter team avatar Alex Fokas avatar

trigger's Issues

time condition

Hi,
how can i use trigger for pre-defined time, once a day?
thanks :)

Persistable repeated job is executed twice

Hi there, I just found out this awesome library.... great job, it just takes 15 minutes to implement...

however, please check your sample...

    public static class PersistAfterRebootWithChargingAction extends ContextAction {

        @Override
        protected void act(Context context) {
            Log.v("mylog", "act " + System.currentTimeMillis());
            Toast.makeText(context, "Charging...(from persist Job after reboot)", Toast.LENGTH_SHORT).show();
        }
    }

I put a log there and it's executed twice if repeat() is used

Is Example wokring fine?

I copied the code from the example:

Job persistAfterRebootJob = new Job(true, new PersistAfterRebootWithChargingAction())
                .attachOn(ThreadSpace.MAIN)
                .repeat(71 * 60 * 1000)
                .needCharging(true);
        trigger.schedule(job1, job2, job3, persistAfterRebootJob);

Since you have not supplied any condition i assume that it does not require.

But i am getting the exception:

java.lang.IllegalArgumentException: Please check your Job, it can not be triggered at all without any conditions.

Please correct the example or me :)

Error:Could not find trigger.jar

I tried to use your library using gradle. But Gradle sync failed. The error message is given below:

Error:Could not find trigger.jar (com.github.airk000:trigger:1.1.0). Searched in the following locations: https://jcenter.bintray.com/com/github/airk000/trigger/1.1.0/trigger-1.1.0.jar

What should I do?

App not running

Hello,
I write this simple code:

default
(breakpoint not called)

And app not running. If i remove this code, everything works.
I add service into manifest :(
Whats I`am doing wrong?

Device with - Android L

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.