Service that runs every minute - android

I have a service that I am wanting to execute a task every minute in the background. It does not need to execute the task whenever the phone is asleep, only when the user is actively using it. I am trying to do this with an IntentService which is set up as follows:
public class CounterService extends IntentService{
public CounterService() {
super("CounterService");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, "onhandleintent", Toast.LENGTH_SHORT).show();
while(true)
{
//one minute is 60*1000
try {
Thread.sleep(5 * 1000);
Toast.makeText(getApplicationContext(), "getting app count", Toast.LENGTH_LONG).show();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Right now to get the functionality working I simply want it to display a toast every 5 seconds, I will change it to one minute later. If I have the while(true) commented out, then the "onhandleintent" message is displayed. However if I have the following code run, neither of the Toasts display. How can I fix this?

This will send an intent to your service every minute without using any processor time in your activity in between
Intent myIntent = new Intent(context, MyServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 60); // first time
long frequency= 60 * 1000; // in ms
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), frequency, pendingIntent);
Adjust MyServiceReceiver.class to match your target service or activity.
The documentation provides more details to fine-tune your calls like whether you want exact timing, execution at a specific time of the day ...

You need to exit the main thread to avoid risking a ANR.
Instead add a Handler
Handler mHandler = new Handler();
...
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, "onhandleintent", Toast.LENGTH_SHORT).show();
mHandler.postDelayed( ToastRunnable, 5000);
//while(true)
//{
//one minute is 60*1000
//try {
// Thread.sleep(5 * 1000);
// Toast.makeText(getApplicationContext(), "getting app count",
//Toast.LENGTH_LONG).show();
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
//}
//}
}
final Runnable ToastRunnable = new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "getting app count",
Toast.LENGTH_LONG).show();
mHandler.postDelayed( ToastRunnable, 5000);
}
}

Do it like this
private void ping() {
try {
//Your code here or call a method
} catch (Exception e) {
Log.e("Error", "In onStartCommand");
e.printStackTrace();
}
scheduleNext();
}
private void scheduleNext() {
mHandler.postDelayed(new Runnable() {
public void run() { ping(); }
}, 60000);
}
public int onStartCommand(Intent intent, int x, int y) {
mHandler = new android.os.Handler();
ping();
return START_STICKY;
}

Related

Services - Broadcasting between activity and service

I know there are many threads on Stackoverflow on this topic, and I have read each and every single one, but I am still very confused and am very sorry but I feel like I should post this.
When the application is run nothing really happens with the receivers, as if they are not receiving anything
My service:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
int timerValue = pv.getTimerValues();
aCounter = new CountDownTimer((timerValue), 1000) { // set to
// timerValue
// later
public void onTick(long millisUntilFinished) {
int minutes, seconds;
String sSeconds, sMinutes, toSend;
seconds = (int) (millisUntilFinished / 1000) % 60;
minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
sSeconds = Integer.toString(seconds);
sMinutes = Integer.toString(minutes);
toSend = (sMinutes + ":" + sSeconds);
sendMessage(toSend);
countDownCheck();
}
public void onFinish() {
MediaPlayer dingeffect = MediaPlayer.create(
getApplicationContext(), R.raw.timerfinished);
// mPlayer0.stop();
try {
dingeffect.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dingeffect.start();
// Intent intent2 = new Intent(ServiceTest.this, Meditate.class);
//startActivity(intent2);
sendMessage("finished");
}
};
aCounter.start();
return START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
private void sendMessage(String send) {
Intent intent1 = new Intent("countdowntimer");
// You can also include some extra data.
intent1.putExtra("message1", send);
sendBroadcast(intent1);
}
My receiving activity:
Inside the onCreate:
registerReceiver(mMessageReceiver1, new IntentFilter("countdowntimer"));
final Intent countDownService = new Intent(Meditate2.this,
ServiceTest.class);
startService(countDownService);
and my broadcastreceiver inside the same activity
private BroadcastReceiver mMessageReceiver1 = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message1");
setTimer(message);
}
};
In the manifest I have declared the service as such:
<service android:name=".ServiceTest"></service>
The weird thing is, it was working then after I fixed some other stuff this stopped working, so I really have no clue why it is not. Help please!
I will tell you what is different between your code and my (working) code.
In the activity onCreate:
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver1, new IntentFilter("countdowntimer"));
In the activity onDestroy:
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver1);
In sendMessage:
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
I don't know if this makes a lot of difference, but I used LocalBroadcastManager.getInstance.
Your broadcast receiver should know what intents to listen to.
Register your receiver with an intentFilter that listens to your countdowntimer intent like this:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("countdowntimer");
activity.registerReceiver(this, intentFilter);

Android Service is creating again and again

I have a button on which following code is written
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent intent = new Intent(Formact.this, MyService.class);
MyService.pintent = PendingIntent.getService(Formact.this, 0, intent, 0);
MyService.alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
MyService.alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), 5000, MyService.pintent);
now once the services is created it has to destroy on a specific action but after each time it destroy it starts again.
here is my service class
public class MyService extends Service {
public static int counter = 0;
public static PendingIntent pintent;
public static AlarmManager alarm;
Boolean save=false;
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
return new Binder() ;
}
#Override
public void onCreate() {
Toast.makeText(this, "Service was Created", Toast.LENGTH_SHORT).show();
}
#Override
public void onStart(Intent intent, int startId) {
counter++;
Toast.makeText(this, " Service Started" + " " + counter, Toast.LENGTH_SHORT).show();
SaveForm handler = new SaveForm(getApplicationContext());
handler.setobj(getApplicationContext());
handler.setText(Formact.sendform, Formact.listString);
handler.stratConnection();
String m = "";
int val = 0;
try{
Log.e("val",SaveForm.msg);
if(SaveForm.msg!=null)
{
m=SaveForm.msg.substring(SaveForm.msg.length() - 1);
}
val=Integer.parseInt(m);
Log.e("val",m);
if(val>0)
{
Toast toast = Toast.makeText(getApplicationContext(), "Data saved", 100);
toast.show();
save=true;
MyService.this.stopSelf();
// alarm.cancel(pintent);
if(alarm!=null)
{
try{
alarm.cancel(pintent);
}
catch(Exception e)
{
Toast toasdst = Toast.makeText(getApplicationContext(), "Massi", 100);
toasdst.show();
}
alarm=null;
}
}
}
catch(Exception e)
{
Toast toast = Toast.makeText(getApplicationContext(), "Data Not saved", 100);
toast.show();
///responseStr = responseStrr;
}
}
#Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
if(save)
{
try{
stopSelf();
}
catch(Exception e)
{
Toast.makeText(this, "Head Bang", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
}
}
I had set the alarm.cancle but it throws exception because alarm is already null
I had also tried this
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try{
// String parameter = intent.getStringExtra("param_name");
if(save){
stopSelf();
}
}catch(Exception ex){
}
return startId;
}
But nothing works service starts again and again.
One more thing if I did not close the application than every thing works perfect the alarm is canceled but when I close the application and expect to run it same it background it start creating again and again.
Help please.
1.Do not use onStart(), in docs:
onStart(Intent intent, int startId)
This method was deprecated in API level 5. Implement onStartCommand(Intent, int, int) instead.
Use onStartCommand instead and return START_NOT_STICKY or START_STICKY not startId.
2. It seems that you are starting a connection handler.stratConnection(); if you are binding anything to it using bindService(), then you need to unbind it from the service using unbindService(mConnection). As given in docs:
Disconnect from an application service. You will no longer receive calls as the service is restarted, and the service is now allowed to stop at any time.
If you are not binding anything then return null in onBind().
#Override
public IBinder onBind(Intent intent) {
return null;
}
3. Use super.onDestroy() like this:
#Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
/***** No need to put this as it is already going to be destroyed
if(save)
{
try{
stopSelf();
}
catch(Exception e)
{
Toast.makeText(this, "Head Bang", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
}
*****/
}
4. Cancel the alarm by alarm.cancel(pintent) before calling stopSelf().
if(alarm!=null)
{
try{
alarm.cancel(pintent);
}
catch(Exception e)
{
Toast toasdst = Toast.makeText(getApplicationContext(), "Massi", 100);
toasdst.show();
}
alarm=null;
}
MyService.this.stopSelf();
5. You are using alarm and pintent without initializing it. It is not initialized in your code.
6. Stop service in your activity by using context.stopService(intent), using the same intent as Intent intent = new Intent(Formact.this, MyService.class);.
As you are storing the data to the server by using
handler.setText(Formact.sendform, Formact.listString);
Simply once the data is saved make
Formact.sendform=null
Formact.listString=null
when these values are null the application will throw a exception catch it in a catch block and use this line to close
MyService.alarm.cancel(MyService.pintent);
MyService.this.stopService();
service will not start again.

Why didn't accelerometer start on time or retrieve data normally?

I write an app that schedule to start accelerometer every 15 seconds and run it 10 seconds to retrieve sensor data. I start accelerometer using alarmManager and keep it running with wakelock when screen go off. But I found sometimes:
The accelerometer can't retrieve data after some periods. I mean it have been started according to logcat,but there is not any sensor data,then it is closed and it starts again without data.
I don't know why the accelerometer didn't start any more after some periods and it is fine before. I found logcat says "listener with binder android.os.BinderProxy doesn't exist accelerometer". I don't know why.
following is kernel code:
Activity.java
Intent intent = new Intent(getApplicationContext(),SensorService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long curTime = Calendar.getInstance().getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, curTime, _intervalTime, pendingIntent);
SensorService.java
public void onStart(Intent intent, int startId) {
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
wl.acquire();
Log.d(TAG,"I'm bright!");
try {
Thread.sleep(_delayClose);// kill self after _delayClose ms
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stopSelf();// SensorService.this.stopSelf()
}
}).start();
this.mSensorManager.registerListener( _sensorHandle,
accSensor,
sensorDelay)
}
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
this.mSensorManager.unregisterListener(_sensorHandle);
Log.d(TAG,"TIEM UP");
if(wl.isHeld())
{
wl.release();
}
}
I'm guessing that there must be some considerable overhead in registering and unregistering sensor listener that often, plus the alarmManager overhead.
Given that you want sensor data that often, how about just keep your listener registered and have it internally manage when it should and shouldnt do something useful with the samples it's receiving?
public class SensorSamplrActivity extends Activity {
private final static String TAG = "samplr";
private final static int SAMPLE_INTERVAL_SECS = 15;
private final static int SAMPLE_DURATION_SECS = 10;
private SensorManager mSensorManager;
private long whenToStartSample = System.currentTimeMillis();
private long whenToEndSample = System.currentTimeMillis() + SAMPLE_DURATION_SECS;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_GAME);
}
private SensorEventListener mSensorListener = new SensorEventListener() {
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
long now = System.currentTimeMillis();
if (now < whenToStartSample){
// ignore the event & wait for next time to sample
Log.d(TAG,"ignoring events for " + (whenToStartSample - now) + "ms");
return;
}
if (whenToStartSample <= now && whenToEndSample > now){
Log.d(TAG,"Do something with this event # " + now);
}
else {
// we've gone past whenToEndSample so reset timers
whenToStartSample = now + (SAMPLE_INTERVAL_SECS * 1000);
whenToEndSample = whenToStartSample + (SAMPLE_DURATION_SECS * 1000) ;
}
}
};
#Override
public void onDestroy(){
super.onDestroy();
mSensorManager.unregisterListener(mSensorListener);
}
}

Error Thread already started / scheduled

Friends,
I set up an AlarmManager within my application. The AlarmManager is scheduled to start a background Service every xx , here 1 Min. Its working quite well for a while. But freuqently I get an Error: thead already started / scheduled.
I have the feeling that i might dont use destructors correct.
Would be grateful for your support.
Heres my code of the Activity that starts the AlarmManager
PendingIntent pi;
AlarmManager mgr;
mgr=(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(DataCollectionActivity.this, HUJIDataCollectionService.class);
pi = PendingIntent.getService(DataCollectionActivity.this, 0, i, 0);
........
if (viewId == R.id.b_startService) {
mgr.cancel(pi);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() , 1* 60* 1000, pi);}
........
if (viewId == R.id.b_stopService) {
mgr.cancel(pi);}
and heres the important code of my Service:
private Runnable LocationUpdateTimerTask = new Runnable() {
public void run() {
Log.i(ctx.getString(R.string.app_name),
"HUJIDataCollectionService, 1 LocationUpdateTimerTask, start");
setuplistenerandrequestupdates();
mHandler.removeCallbacks(LocationUpdateTimerTask);
}
};
private Runnable SendDataStopLocationUpdatesTimerTask = new Runnable() {
public void run() {
sendDataToServer();
mHandler.removeCallbacks(SendDataStopLocationUpdatesTimerTask);
ServiceIntervalTimerTask.cancel();
Intent service = new Intent(ctx, HUJIDataCollectionService.class);
stopService(service);
}
};
private TimerTask ServiceIntervalTimerTask = new TimerTask() {
#Override
public void run() {
// remove old timer updates
mHandler.removeCallbacks(LocationUpdateTimerTask);
mHandler.removeCallbacks(SendDataStopLocationUpdatesTimerTask);
// Start TimerTasks delayed
mHandler.postDelayed(LocationUpdateTimerTask, 1000);
mHandler.postDelayed(SendDataStopLocationUpdatesTimerTask,
conf_LocationUpdatePeriodInSec * 1000);
}
};
#Override
public void onDestroy() {
super.onDestroy();
startDataCollectionServiceIntervallTimer.cancel();
startDataCollectionServiceIntervallTimer = null;
// Remove all kinds of updates
mHandler.removeCallbacks(LocationUpdateTimerTask);
mHandler.removeCallbacks(SendDataStopLocationUpdatesTimerTask);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
startDataCollectionServiceIntervallTimer = new Timer(
"HUJIDataCollectionServiceStartTimer");
startDataCollectionServiceIntervallTimer.schedule(ServiceIntervalTimerTask,
1000L, conf_sampleTimeInMin * 60 * 1000L);
mHandler = new Handler();
return START_STICKY;
}
When you start a service it runs in the backround even when app is destroyed. Where and when you call your Alarm manager??? But if you often call a service i think that you will have memory leak or something like that...
I think i found the solution for the problem my own.
First i bypassed the problem by starting a Broadcastreceiver. But this is not an answer to the described problem.
Here is a solution:
public void pause(){
while(true){
try { // goes through this thread until our thread died
ourthread.join(); //Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies.
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
Thanks for the support anyways!!!
Cheers

countDownTimer isn't working, why?

I have a service B that sends a specific number of messages in a fixed interval.
this service is called from another service A.
the code used in service A is
#Override
public void onStart (Intent intent,int startid)
{
Toast.makeText(this, "Service A Running onStart", Toast.LENGTH_LONG).show();
Thread MessagesThread = new Thread(new Runnable()
{
public void run()
{
ApplicationPreferences AppPrefs = new ApplicationPreferences(getApplicationContext());
int NumberOfMessagesToSend = Integer.parseInt(AppPrefs.getNumberOfMessagesToSend());
int NumberOfSentMessages;
for (NumberOfSentMessages = 0 ; NumberOfSentMessages < NumberOfMessagesToSend; NumberOfSentMessages++ )
{startServiceB();
}
}
});
MessagesThread.start();
}
public void startServiceB()
{
final Intent sendingMessages = new Intent(this, ServiceB.class);
startService(sendingMessages);
}
the toasts are to keep track of what is happening
The code in service B is as follow
#Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Toast.makeText(getApplicationContext(), "Service B at start ", Toast.LENGTH_LONG).show();
new CountDownTimer(30000,1000)
{
public void onTick (long millisUntilFinished) {}
public void onFinish()
{
showToast();
}
}.start();
}
the showToast() function is as follow
public void showToast()
{
Toast.makeText(getApplicationContext(), "Service B in timer", Toast.LENGTH_LONG).show();
}
As I said I am using the toasts to keep track of what's happening. the problem is when running it, i am getting the first toast (service B at start) 10 times consequently then the second one (service B in timer) 10 times consequently with no time between them.
how do i make each of this toasts appear once every 30 seconds?
Ok, so the final answer could be something like this:
Call only once the B service and in it we will have the handler that will loop at an interval of 30 seconds..
Service B code:
int loop = 5;
int counter = 0;
Handler myHandler;
Runnable run;
#Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Toast.makeText(getApplicationContext(), "Service B at start ", Toast.LENGTH_LONG).show();
myHandler = new Handler();
run = new Runnable()
{
public void run()
{
if (counter<loop){
showToast();
counter++;
} else {
myHandler.removeCallbacks(run);
}
}
};
myHandler.postDelayed(run, 30000);
}
I hope this helps someone else too!
If you want to make a toast every 30 seconds than you can do it by using a handler:
Handler myHandler = new Handler();
Runnable run = new Runnable()
{
public void run()
{
showToast();
}
};
myHandler.postDelayed(run, 30000);
If you have problem with this just post here and I will try to help you..

Categories

Resources