简体   繁体   中英

How to detect android phone 3G On Off events

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 BroadcastReceiver s. 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 in TelephonyManager.DATA_CONNECTED and TelephonyManager.DATA_DISCONNECTED events. You can distinguish between the different types of data networks (2G, EDGE, 3G, etc) by querying the network type of the TelephonyManager .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM