I am writing an android application that handles 3G usage .. When the user turn on the 3G Mobile Data Connection , I want to calculate the elapsed time of 3G usage and notify the user when the elapsed time comes to a specific time..
The problem is I don't know how to detect the 3G On Off events in order to run certain codes when those events occur. Please help me with a detailed example as I am a beginner to android.
I have tried googling for it but does not help me. I would like to call a function like this one:
public void function on3GOnCallback(){
// i will start counting the time here
}
public void function 0n3GOffCallback(){
// `i will stop counting the time here `
}
System level callbacks are implemented using
BroadcastReceivers. Please refer Android BroadcastReceiver Tutorial for more info.The events for mobile data state are broadcast by
TelephonyManager. In particular, you seem to be interested inTelephonyManager.DATA_CONNECTEDandTelephonyManager.DATA_DISCONNECTEDevents. You can distinguish between the different types of data networks (2G, EDGE, 3G, etc) by querying the network type of theTelephonyManager.