Check the service on several time gaps.

Now three...

Hope this will be very useful for those who doing time intervals and web services.

ServiceChecker.Java 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class ServiceChecker extends BroadcastReceiver {
    ShukranDbHelper dbHelper;
    @Override
    public void onReceive(Context context, Intent arg1) {
        // Just calling your method
Log.d("hey", "I'm alive");
    }
}


In AndroidManifest.xml

<receiver android:name=".service.DataChecker"></receiver>

In your Activity, Just call this.

Intent alarmIntent = new Intent(this, ServiceChecker.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
startAlarm();


public void startAlarm() {
    manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval =600000; // 10 minute //10000
    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
}

public void cancelAlarm() {
    if (manager != null) {
        manager.cancel(pendingIntent);
        Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
     }
}

Comments

Popular posts from this blog

Clear the app data programmatically in android

convert an integer to a string in java

Print the large string values in android log cat.