Android How to execute a method every x hours or minutes
I'm trying to run a method called Updater()
using an alarm service, and for testing purposes, I want it to update every 1 minute and eventually every 2 hours. The Sceduled task got knocked over on the timer if the toggle button is on and stops the timer when the toggle button is off this is my code so far. Can someone show what I am doing wrong?
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Updater();
}
}
public void autoUpdateClick(View view)
{
AlarmManager alarmManager=(AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
boolean on = ((ToggleButton) view).isChecked();
if (on)
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),30000,pendingIntent);
Toast.makeText(getBaseContext(),"Check-In will be done every 2 hours",Toast.LENGTH_SHORT).show();
}
else
{
alarmManager.cancel(pendingIntent);
Toast.makeText(getBaseContext(),"Manual Check-In enabled",Toast.LENGTH_SHORT).show();
}
}
source to share
As you mentioned, we are scheduling an alarm to run the method at a specific time in the future.
We will have two classes
- MainAcitvity: in this class, we plan to set the alarm to go off at a specific time.
- AlarmReciever: when the alarm triggers at the scheduled time, this class will receive the alarm and execute the method.
The AlarmReciever class extends BroadcastReceiver and overrides theRecieve () method. inside onReceive () you can start a job or service depending on your needs, for example you can start an activity to vibrate the phone or make a phone call.
Permission required we need permission to use AlarmManger in our app so don't forget to declare permission in manifest file
AndroidManifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.src"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- permission required to use Alarm Manager -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:icon="@drawable/ic_launcher"
android:label="Demo App" >
<activity
android:name=".MainActivity"
android:label="Demo App" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Register the Alarm Receiver -->
<receiver android:name=".AlarmReciever"/>
</application>
</manifest>
main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView1"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Alarm Manager Example"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_marginTop="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Schedule The Alarm"
android:onClick="scheduleAlarm"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void scheduleAlarm(View V)
{
// time at which alarm will be scheduled here alarm is scheduled at 1 day from current time,
// we fetch the current time in milliseconds and added 1 day time
// i.e. 24*60*60*1000= 86,400,000 milliseconds in a day
Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;
// create an Intent and set the class which will execute when Alarm triggers, here we have
// given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
// alarm triggers and
//we call the method inside onRecieve() method pf Alarmreciever class
Intent intentAlarm = new Intent(this, AlarmReciever.class);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
}
}
AlarmReciever.java
public class AlarmReciever extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//call the method here
}
}
source to share