-2

I would like to create a (test) app that executes a command every hour in the background (for example, edit a file or show a debug message).

Assuming that the app starts with system boot

  • How can I do this?
  • The app must remain running always?
Cœur
  • 37,241
  • 25
  • 195
  • 267
Simone Sessa
  • 795
  • 1
  • 12
  • 35
  • *The app must remain running always* good luck with that. A user can terminate your app whenever he wants to – Tim Oct 07 '15 at 09:29
  • Possible duplicate of [How to execute one task every hour?](http://stackoverflow.com/questions/10630365/how-to-execute-one-task-every-hour) – Tim Oct 07 '15 at 09:32
  • I expressed myself badly, I mean that the app should NOT remain in the background, it was a question. Sorry for duplicate, I didn't find that argument. – Simone Sessa Oct 07 '15 at 15:19

2 Answers2

0

Please use alarmmanager using service functionality to achieve this.

for eg:

private void setAlarmManager() {
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(this, 2, intent, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    long l = new Date().getTime();
    if (l < new Date().getTime()) {
        l += 86400000; // start at next 24 hour
    }
    am.setRepeating(AlarmManager.RTC_WAKEUP, l, 86400000, sender); // 86400000
}

Source

Community
  • 1
  • 1
-1

To Make your app start up when the devices boot completes You have to create a Class that extends Receiver. In the manifest Declare your class like this

<receiver android:name=".ClassName">
<intent-filter >
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

And don't forget to use the permission

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Write the following to your Receiver class, onReceive Override method

Intent intent= new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);

If you want to Run something in your background use Services

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • OP doesn't want to know how to start his app on boot --> *Assuming that the app starts with system boot* – Tim Oct 07 '15 at 09:33
  • Then kindly down vote the question bro Not my answer please.... You know everything that doesn't mean our answers are wrong and eligible for downvoting... – Shree Krishna Oct 07 '15 at 10:29
  • If your answer doesn't address the problem in the question, it's "not useful" which makes it eligible for downvoting. Of course you can misinterpret the question and give a wrong answer, I do that myself some times. In that case I just admit my mistake and delete my answer. You are free to do as you like of course – Tim Oct 07 '15 at 10:31
  • I mean to say that you are not downvoting the answers that was copied from duplicated question even you already gave the source.. But u did mine....But anyway Thanks for the info.... – Shree Krishna Oct 07 '15 at 10:37
  • Because that other answer does address the question, I consider it useful, even though it was copied from another answer (so I didn't upvote either). I hope you understand my reasoning – Tim Oct 07 '15 at 10:45
  • Yah sure... We are new at stackoverflow... You are Senior then us... Thanks... I understood... – Shree Krishna Oct 07 '15 at 10:51