I want to understand the concept of callback. I have searched on internet about the callbacks and there are many examples using interface, and one class is calling a method of another class using that interface. But still I can't get the main concept of callbacks, what is the purpose of using callbacks?
Answers
SharedUserId is used to share the data,processes etc between two or more applications. It is defined in AndroidManifest.xml like,
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:sharedUserId="android.uid.shared"
android:sharedUserLabel="@string/sharedUserLabel"
...>
and define the shared parameter in Android.mk for that app, like
LOCAL_CERTIFICATE := shared
Hope its helpful to you.
R.color.whatever
is an int. It's automatically generated as a reference to an externally defined (in XML) resource. When you call setBackgroundColor
with this integer, it's trying to parse this int's value as a Color. setBackgroundResource
expects to get a resource integer passed to it. It retrieves the externally defined value, and then applies the color that way. As for setBackgroundColor
, try using a full hex value color with alpha included, e.g. 0xFFFFFFFF (where the first two F values are the alpha value).
EDIT: Beaten by Mark! :P
I think you have 2 options:
The fake progress bar approach
You know in advance how many files you need to download, you can set the ProgressDialog
total amount to the number of files to download. This works pretty well with files which are small and similar in dimensions and gives the user a good feedback about what's going on.
// you can modify the max value of a ProgressDialog, we modify it
// to prevent unnecessary rounding math.
// In the configuration set the max value of the ProgressDialog to an int with
pDialog.setMax(urls.length);
for (int i = 0; i < urls.length; i++) {
// launch HTTP request and save the file
//...
// your code
//...
//advance one step each completed download
publishProgress();
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(Integer... progress) {
pDialog.incrementProgressBy(1);
}
The real progress bar approach
You need to know in advance the total length of all the files you need to download. For example, you could create a separate REST API to call before everything else which give you the total length in bytes before you start to download each separate files. In this way, you can periodically update the total ProgressDialog
length accordingly to the total bytes you have already downloaded.
do it with a receiver. you can be notified about network state change. for example,
private BroadcastReceiver networkReceiver = new BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getExtras()!=null) {
NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
// we're connected
}
}
// we're not connected
}
}
register this in your onResume()
, and unregister on onPause()
.
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(networkReceiver);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkReceiver, filter);
}
additionally, to obtain the initial state before your receiver has been registered, call this in your onResume()
method,
public boolean isNetworkConnected() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
make sure your app requests this permission,
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Here is a nice tutorial, which describes callbacks and the use-case well.
The concept of callbacks is to inform a class synchronous / asynchronous if some work in another class is done. Some call it the Hollywood principle: "Don't call us we call you".
Here's a example:
Class A calls Class B to get some work done in a Thread. If the Thread finished the work, it will inform Class A over the callback and provide the results. So there is no need for polling or something. You will get the results as soon as they are available.
In Android Callbacks are used f.e. between Activities and Fragments. Because Fragments should be modular you can define a callback in the Fragment to call methods in the Activity.