Schedule a future event (Cron Job the Android)

I have certain dates that once reached their relevance, and new dates for these fields in the database must be calculated, I know that I can use the AlarmManager class for this, however I have several problems in this regard:

1) Note. Starting with the alarm delivery API 19 (KITKAT) is inaccurate: the OS resets alarms to minimize wake-ups and battery usage. There are new APIs to support applications that require strict delivery guarantees; see setWindow (int, long, long, PendingIntent) and setExact (int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see previous behavior in which all alarms are sent exactly on demand.

So do I need to code both cases separately or if I am targeting kitkat, will this work for older versions too? In addition, since the execution of my code is time critical, say, after 12 a.m. at midnight of some date, my data is no longer relevant, how to overcome the switching of alarms.

2) The registered alarms are saved when the device is sleeping (and if necessary can activate the device if it is turned off during this time), but will be cleared if it is turned off and rebooted.

2.1) Set the RECEIVE_BOOT_COMPLETED permission to the application manifest. This allows your application to receive ACTION_BOOT_COMPLETED, which is broadcast after the system boots up (this only works if the application has already been run by the user at least once)

2.1.1) If I set the alarm to 12, the service associated with this alarm is triggered at 12. Now, when I reboot the device, the time at 12 has already passed, will the alarm start again and the service will be called again?

, ? , ?

-, , , , , ?

, , 12 , , , 12, ?

EDIT: :

, , 12:00. ( , ), . PARTIAL_WAKE_LOCK , . , . MainActivity, 12 ( ):

public class MainActivity extends Activity {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    BroadcastReceiver br;
    TextView t; 
    int sum; 


    public void setSum(int s){
        sum = s; 

    //  t = (TextView)findViewById(R.id.textView1);
    //  t.setText(sum);
        System.out.println("In Set Sum"+s);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setup(); 
        t = (TextView)findViewById(R.id.textView1);

        ComponentName receiver = new ComponentName(getApplicationContext(), SampleBootReceiver.class);
        PackageManager pm = getApplicationContext().getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);


        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 17);
        calendar.set(Calendar.MINUTE, 05); // Particular minute
        calendar.set(Calendar.SECOND, 0);
        alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                1000*60*60*24, alarmIntent);
    }


    public void setup() {
        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent i) {
                Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
                //Invoke the service here Put the wake lock and initiate bind service
                t.setText("Hello Alarm set");

                startService(new Intent(MainActivity.this, MyService.class));
                stopService(new Intent(MainActivity.this, MyService.class));

            }
        };
        registerReceiver(br, new IntentFilter("com.testrtc") );
        alarmIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.testrtc"),0 );
        alarmMgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

SampleBootReceiver, Alarms , , . , , Toast .

public class SampleBootReceiver extends BroadcastReceiver {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    BroadcastReceiver br;
    TextView t; 
    MainActivity main; 

    @Override
    public void onReceive(Context context, Intent intent) {
        main= new MainActivity(); 
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Toast.makeText(context, "Hello from Bootloader", 10000).show();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 15);
            calendar.set(Calendar.MINUTE, 50); // Particular minute
            calendar.set(Calendar.SECOND, 0);
            alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    1000*60*60*24, alarmIntent);

            context.getApplicationContext().registerReceiver(br, new IntentFilter("com.testrtc") );
            alarmIntent = PendingIntent.getBroadcast( context.getApplicationContext(), 0, new Intent("com.testrtc"),
                    0 );
            alarmMgr = (AlarmManager)(context.getApplicationContext().getSystemService( Context.ALARM_SERVICE ));

        }
    }

}

, , onStartCommand:

public class MyService extends Service {

    int a = 2; 
    int b = 2; 
    int c = a+b; 


    public MainActivity main = new MainActivity();

    public MyService() {
    }



    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public int onStartCommand(Intent i, int flags , int startId){
        WakeLock wakeLock = null;

        try{
            PowerManager mgr = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
            wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
            wakeLock.acquire();

            Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();
             new Thread(new Runnable() { 
                    public void run(){


                        //Will be substituted with a time consuming long task. 

                        main.setSum(c); 

                    }
            }).start();

        }catch(Exception e){
            System.out.println(e);
        }finally{

            wakeLock.release();

        }

        return 1; 

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

    }
}

, , , , , . , async wakelock onPostExecute?

, :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testrtc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.testrtc.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".SampleBootReceiver"
            android:enabled="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>
            </intent-filter>
        </receiver>

        <service
            android:name="com.testrtc.MyService"
            android:enabled="true"
            android:exported="true" >
        </service>

    </application>

</manifest>

Cat , , , , epackage:

01-22 15:18:35.652: V/ActivityManager(419): getTasks: max=1, flags=0, receiver=null
01-22 15:18:35.652: V/ActivityManager(419): com.xxx.xxx/.MainActivity: task=TaskRecord{425c58f0 #5 A com.xxx.xxx U 0}
01-22 15:18:35.653: V/ActivityManager(419): We have pending thumbnails: null

: , onCreate Splash, , , , .

-, , , , ? ( , ).

.

+4
2

1) 2 :  a) SDK 18, 19. kitkat  b) setExact(), Kitkat,   : http://developer.android.com/about/versions/android-4.4.html

2) - . Broadcast, . .

2.1). , , , , .. , .

3) SD-,

4) / , 12AM. , , . SQLite, : http://www.sqlite.org/lockingv3.html

+1

, Service ( 5 ) Service BroadcastReceiver, BOOT_COMPLETED.

public class YourService extends IntentService {

    public YourService() {
        super("YourService");
    }

    public YourService(String name) {
        super(name);
    }

    private static Timer timer = new Timer();

    private class mainTask extends TimerTask {
        public void run() {
            // TASK WHATEVER YOU WANT TO DO 
        }
    }

    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        timer.scheduleAtFixedRate(new mainTask(), 0, 300000);  // 5 minutes
    }
}
-1

All Articles