Android Run Method Each 5 Minues
Solution 1:
Put this in the oncreate, it will not only execute once, I am absolutely positive.
TimermyTimer=newTimer();
myTimer.schedule(newTimerTask() {
@Overridepublicvoidrun() {
tcpClient.execute();
}
}, 0, 300000);
Solution 2:
I'm working on a similar type app. I use a broadcastreceiver and a service that schedules it using the alarmmanager, that way you don't have much overhead, like running a timer, which has to stay active.
Here's my main activity. I initiate the sequences with buttons.
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
@Overridepublic void onClick(View v) {
if (v == btn_splash) {
initiateAlarm(true);
Toast.makeText(MainActivity.this, " alarm scheduled", Toast.LENGTH_LONG).show();
}
} elseif (v == btn_cancel)
{
initiateAlarm(false);
Toast.makeText(MainActivity.this, " alarm stopped", Toast.LENGTH_LONG).show();
}
}
};
}
public void initiateAlarm(Boolean bactive) {
alarmUp = false;
alarmUp = (PendingIntent.getBroadcast(getBaseContext(), 0,
new Intent(this, YourService.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
Log.d("myTag", "Alarm is already active");
Toast.makeText(this, " before scheduling Alarm is already active", Toast.LENGTH_LONG).show();
} else {
YourReceiver.scheduleAlarms(this, prefDuration, bactive);
Toast.makeText(this, "alarms were just scheduled", Toast.LENGTH_LONG).show();
}
alarmUp = false;
alarmUp = (PendingIntent.getBroadcast(getBaseContext(), 0,
new Intent(this, YourService.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
Log.d("myTag", "Alarm is already active");
Toast.makeText(this, " after scheduling Alarm is already active", Toast.LENGTH_LONG).show();
}
}
}
Here's the YourReceiver that schedules the service in the alarmmanager.
publicclassYourRecieverextendsBroadcastReceiver {
privatestaticfinalStringTAG="YourReciever";
@OverridepublicvoidonReceive(Context ctxt, Intent i) {
context = ctxt;
scheduleAlarms(ctxt, (long) 3600001, true); // 1 hour - not used
}
staticvoidscheduleAlarms(Context ctxt, Long duration, Boolean bactive) {
Log.e(TAG, " ... scheduleAlarms ");
SharedPreferences preferences;
preferences = PreferenceManager.getDefaultSharedPreferences(ctxt);
prefDuration = preferences.getLong(PREF_DURATION, 3600000); // 1 hour
Log.e(TAG, " ... onReceive ... duration: " + duration);
AlarmManager mgr=
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=newIntent(ctxt, YourService.class);
PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);
mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + duration, duration, pi);
if (bactive == false) {
mgr.cancel(pi);
}
}
}
and finally here's the service class
publicclassYourServiceextendsIntentService {
publicYourService() {
super("YourService");
Log.e(TAG, " ... YourService");
}
@OverrideprotectedvoidonHandleIntent(Intent intent) {
//// The action you want to perform.//
}
}
Something like this needs be in the manifest.
<serviceandroid:name="com.yourpackagename.YourService"android:exported="true"/>
Hope this helps.
Have a good day.
Solution 3:
Try using the Action time tick broadcast. Here's an article that'll show you how. Very simple.
http://androidbridge.blogspot.ca/2011/08/how-to-use-actiontimetick-broadcast.html
That ticks every minute, so then in the reciever you just want to include something like
tick++
if(tick>=5)
{
//Do what you want to do here
tick=0;
}
else
{
return;
}
Solution 4:
Use this code it will help u call a function modify it according to u r requirement
private final long refreshDelay = 5 * 1000;
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
sendHttpRequestThread = newSendHttpRequestThread("","");
sendHttpRequestThread.start();
}
classSendHttpRequestThreadextendsThread {
boolean sendHttpRequest;
String userId;
String pass;
publicSendHttpRequestThread(String str1, String str2) {
this.userId = str1;
this.pass = str2;
sendHttpRequest = true;
}
publicvoidstopSendingHttpRequest() {
sendHttpRequest = false;
}
protectedvoidonStop() {
sendHttpRequestThread.stopSendingHttpRequest();
super.stop();
}
@Overridepublicvoidrun() {
while (sendHttpRequest) {
//Call U r function from here SystemClock.sleep(refreshDelay);
}
}
}
Post a Comment for "Android Run Method Each 5 Minues"