Boa tarde a todos!!
estou com um problema e preciso de ajuda.
tenho uma aplicação que executa algumas ações de tempo em tempo, utilizo um handler e um service para executar essas ações. porem quando o celular ta bloqueado ou fora da usb, o serviço para, ai quando eu volto a utilizar a aplicação o android dispara todos de uma só vez, ai fica uma pancada de eventos acumulados.
abaixo um exemplo que eu fiz
package br.teste;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TimerTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent("SERVICE_TIMER"));
}
}
package br.teste;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
public class TimerService extends Service{
Timer timer = new Timer();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
String horaAgora = new SimpleDateFormat("HH:mm:ss").format(new Date());
System.out.println("Startando Service "+horaAgora);
// final TimerTask task = new TimerTask() {
//
// @Override
// public void run() {
// String horaAgora = new SimpleDateFormat("HH:mm:ss").format(new Date());
// System.out.println("Timer Ativado... "+horaAgora);
// stopSelf();
// sendBroadcast(new Intent("ABRIR_RECEIVER"));
// }
// }; timer.schedule(task, 10000, 10000);
final Runnable runnable = new Runnable() {
@Override
public void run() {
String horaAgora = new SimpleDateFormat("HH:mm:ss").format(new Date());
System.out.println("Timer Ativado... "+horaAgora);
stopSelf();
sendBroadcast(new Intent("ABRIR_RECEIVER"));
}
};
Handler handler = new Handler();
handler.postDelayed(runnable, 60*1000);
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Parando serviço...");
// timer.cancel();
}
@Override
public void onLowMemory() {
super.onLowMemory();
System.out.println("Memoria Baixa...");
}
}
package br.teste;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class TimerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent("SERVICE_TIMER"));
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.teste"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TimerTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TimerService" >
<intent-filter>
<action android:name="SERVICE_TIMER"/>
</intent-filter>
</service>
<receiver android:name=".TimerReceiver" >
<intent-filter>
<action android:name="ABRIR_RECEIVER"/>
</intent-filter>
</receiver>
</application>
</manifest>