I would like to publish a very useful snipper of code to create cron service in your Android app. Of course you might use some libraries created by other developers. A lot of them might be found on site called Android Arsenal [1] which also I would like to recommend for developers who are looking for libraries.
So, First of all you need to create Handler [2] object which allows you to send and process Runnable objects. This object has a lot of methods which might help you to define expected behavior of your code. This time I will use method postDelayed which allows to execute some Runnable with delay.
private Handler handler = new Handler();
And then we can define Runnable with task in it which should be executed with given delay
Runnable networkReceiverTask = new Runnable() { @Override public void run() { //execute here method with given delay handler.postDelayed(networkReceiverTask, 15000); } };
Reference:
[1] Android Arsenal Job Schedulers
[2] Android Handler
Some of you might be looking for some solution to display progress loading dialog. I have used below example it in one of my Android application compiled against API 23 (Android 6.0). That's why on first glance you might think that style theme which I used in this case is overloaded. You need just to adjust it to your own purposes.
<style name="LoadingDialogTheme"> <item name="colorAccent">#1976D2</item> <item name="android:textColor">#1976D2</item> <item name="android:textColorPrimary">#1976D2</item> <item name="android:textColorSecondary">#1976D2</item> <item name="android:windowFrame">@null</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowTitleStyle">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:background">@android:color/transparent</item> </style>
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this, R.style.LoadingDialogTheme); progressDialog.setMessage(getString(R.string.dialog_loading_content)); progressDialog.setCancelable(false); progressDialog.setCanceledOnTouchOutside(true); progressDialog.show();
And the final result is as this one :