I built my own web application a couple of days ago. Along with it, I built in a filter, that blocks some content and needs verification. An API lets me connect to my web application and I can delete or confirm the content. This in mind, I developed an Android application, only for me, to be able to handle all content as fast as possible. Furthermore, the app should be a way to see immediately, if the web-server crashed (because I'm pretty new to web development, this happens more often than I wish).
My goal is now to have a permanent banner in the notification bar of my phone to show me the status of my web application (if it is up or not, and how many pending requests I have.) This is the reason why I do not want to implement push notification
.
I already have a void that does all that, but only when I press a button inside the application. If I turn it off, it never gets updated.
The code structure I have so far:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//Get the button
reloadButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(final View view){
LoadInfo();
}
});
}
public void LoadInfo(){
//Handle server request
//Update the notification panel
}
All of this (including the comments) does work. I just need a way to call this method like all 10 minutes and update the notification panel.
What I tried:
//Inside the onCreate method
AlarmManager alarmMgr;
Intent intent = new Intent(this, com.purplepandagamestudio.zivicontroller.MainActivity.class);
intent.putExtra("LoadInfo", "LoadInfo");
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES, AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);
From the Android Documentation about scheduled alarms, I came to the conclusion an alarm might be the easiest way to achieve my wanted behavior, even though
it may not be the best choice for your app, particularly if you need to trigger network operations.
Despite this statement I thought this might be the way to go, because I'm the only user and the alternatives seem to be pretty difficult to me.
Is there an easy way to just run this void in background, and how can I achieve this, or do I have to structure my application differently?
EDIT
Like Sulabh Kumar suggested, I tried to implement ScheduledThreadPoolExecuter
. My implementation looks like this:
public class MainActivity extends AppCompatActivity {
public void start() throws InterruptedException{
final ScheduledThreadPoolExecutor scheduler = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1);
final ScheduledFuture<?> getInfo = scheduler.scheduleAtFixedRate(new LoadInfoClass(), 0,2, TimeUnit.SECONDS);
scheduler.schedule(new Runnable() {
@Override
public void run() {
getInfo.cancel(true);
scheduler.shutdown();
}
}, 0, TimeUnit.SECONDS);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//Some stuff
try{
start();
}catch (InterruptedException e){
//Handle error
}
class LoadInfoClass implements Runnable{
public void run(){
//Make the request and update everything
}
}
}
This leads to following behavior:
- The function gets called on startup
- When I press the button with
new LoadInfoClass().run();
, everything runs as predicted - But the the method never gets updated. (Even when I don't close the app)
Am I implementing ScheduledThreadPoolerExecuter
wrong?