0

I am developing android application in which I want to do some thing after every 10 sec. even application is closed.For that I implemented a background service which do background task for me.My code structure looks like:

// my main activity
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button start = (Button)findViewById(R.id.serviceButton);
        Button stop = (Button)findViewById(R.id.cancelButton);

        start.setOnClickListener(startListener);
        stop.setOnClickListener(stopListener);

   }

   private OnClickListener startListener = new OnClickListener() {
    public void onClick(View v){
        startService(new Intent(SimpleServiceController.this,SimpleService.class));
    }               
   };

   private OnClickListener stopListener = new OnClickListener() {
        public void onClick(View v){
            stopService(new Intent(SimpleServiceController.this,SimpleService.class));
        }               
      }; 


//simpleservice

   @Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
}


@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}

This is working fine. But what I want to do now when I start service after every 10 sec just give me simple Toast message and when I stop service stop toast messages.

I also tried with AlarmManager.

// main activity....
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void startAlert(View view) {

    Intent intent = new Intent(this, MyBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this.getApplicationContext(), 234324243, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
            + (10 * 1000), pendingIntent);
    Toast.makeText(this, "Alarm set in " + 10 + " seconds",
            Toast.LENGTH_LONG).show();
}



// broadcast receiver
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Don't panik but your time is up!!!!.",
            Toast.LENGTH_LONG).show();

}

}

But I am not able to do it periodically. What is the proper way to implement this? Need help... Thank you...

nilkash
  • 7,408
  • 32
  • 99
  • 176
  • [You need like this?](http://marakana.com/forums/android/examples/60.html) – Praveenkumar Aug 13 '12 at 10:53
  • use [AlarmManager](http://developer.android.com/reference/android/app/AlarmManager.html) `The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running...` ... but running it every 10 sec will drain battery really quick – Selvin Aug 13 '12 at 11:05
  • Thank you Spk for quick reply. But that solution is specific for only media player looping.Is there any way to do it for any task. – nilkash Aug 13 '12 at 11:07
  • @selvin thank you for quick reply. See My question. I made few changes. Can u please help me to do it periodically..... – nilkash Aug 13 '12 at 11:19
  • 2
    you wana something run every 10 sec.? if so, use `setRepeat` instead `set` and use it only once(fx in some Activity where user can click start or after BOOT event(if you wana start your "service" with system boot) if you wana disable repeating call `cancel` ... anyway as i said running something every 10 sec will drain battery you have to ask yourself if it is necessary – Selvin Aug 13 '12 at 11:27
  • Thank you selvin its working fine.... I want one suggestion from u. What I want to do is continuously check which application is currently running. For that I am using ActivityManager and RunningTaskInfo... Is this the good way to do this task in background and check current running task. – nilkash Aug 13 '12 at 11:55
  • few examples how you can achive this => http://stackoverflow.com/questions/3290936/android-detect-when-other-apps-are-launched ... – Selvin Aug 13 '12 at 13:11

1 Answers1

-2

You need something like this:

while (connected){

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        public void run() {
            Toast.makeText(this, "Hi", Toast.LENGTH_SHORT).show();
        }
    }, 10000);
}

And connected is a boolean that you can change on stop method

Lo Juego
  • 1,305
  • 11
  • 12