What is this: This tutorial shows how to use the Android built in access to the Google DrivingDirections API. The first step to create an actual Navigation-System.
Difficulty: 2.5 of 5
Description: Android provides a useful Class to be used, when you want to be notified of changes to the Data Connection State. It is called DataStateIntentReceiver and provides two interesting methods:
If you want to be notified of Connection State Changes, this is of your interest:
Using java Syntax Highlighting
- myDataStateIntentReceiver.notifyConnectionState(CONNECTIONSTATE_WHAT_ID);
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
When you want to be notified of Data-Activity (Incoming/Outgoing Data):
Using java Syntax Highlighting
- myDataStateIntentReceiver.notifyActivity(CONNECTIONACTIVITY_WHAT_ID);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
The Source:
This is a workign Example, I am using in my AndNav!-Application:
Using java Syntax Highlighting
- protected static final int CONNECTIONSTATE_WHAT_ID = 0x1337; // Any unique number
- protected static final int CONNECTIONSTATE_WHAT_ID = 0x1338; // Any unique number
- protected DataStateIntentReceiver dsir; // A class-Field
- /** Initiates the local field <code>dsir</code> a DataStateIntentReceiver to notify this class on changes to the Connection-State.. */
- private void setupForDataStateChanges() {
- this.dsir = new DataStateIntentReceiver(this, new Handler(){
- @Override
- public void handleMessage(Message msg) {
- Log.d(Constants.DEBUGTAG, "DataStateIntentReceiver Msg received.");
- /* Determine what kind of message we are receiving, checking the what-id. */
- if(msg.what == IGPSMapActivity.CONNECTIONSTATE_WHAT_ID){
- DataState ds = IGPSMapActivity.this.dsir.getConnectionState();
- /* Switch on the ConnectionState... */
- switch(ds){
- case CONNECTED:
- //...
- break;
- case CONNECTING:
- //...
- break;
- case DISCONNECTED:
- //...
- break;
- }
- }else if(msg.what == IGPSMapActivity.CONNECTIONACTIVITY_WHAT_ID){
- DataActivityState as = IGPSMapActivity.this.dsir.getActivityState();
- switch(as){
- case DATAIN:
- //...
- break;
- case DATAOUT:
- //...
- break;
- case DATAINANDOUT:
- //...
- break;
- case NONE:
- //...
- break;
- }
- super.handleMessage(msg);
- }
- });
- // Finally tell the System, that we want to be notified of ConnectionState-Changes...
- this.dsir.notifyConnectionState(CONNECTIONSTATE_WHAT_ID);
- this.dsir.notifyActivity(CONNECTIONACTIVITY_WHAT_ID);
- this.dsir.registerIntent();
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
Maybe Eclipse doesn't make the imports automatically --> Here are they:
Using java Syntax Highlighting
- import android.telephony.DataStateIntentReceiver;
- import android.telephony.Phone.DataState;
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
[align=center]Thats it
[/align]
Regards,
plusminus







