DataStateIntentReceiver - Are we connected to the Internet?

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

DataStateIntentReceiver - Are we connected to the Internet?

Postby plusminus » Sun Mar 30, 2008 5:09 pm

[align=center]DataStateIntentReceiver - Is Internet Connection available?[/align]

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.

:idea: Designed/Tested with sdk-version: m5-rc14

:?: Problems/Questions: post right below...

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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myDataStateIntentReceiver.notifyConnectionState(CONNECTIONSTATE_WHAT_ID);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

When you want to be notified of Data-Activity (Incoming/Outgoing Data):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. 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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         protected static final int CONNECTIONSTATE_WHAT_ID = 0x1337; // Any unique number
  3.  
  4.         protected static final int CONNECTIONSTATE_WHAT_ID = 0x1338; // Any unique number
  5.  
  6.  
  7.  
  8.         protected DataStateIntentReceiver dsir; // A class-Field
  9.  
  10.  
  11.  
  12.         /** Initiates the local field <code>dsir</code> a DataStateIntentReceiver to notify this class on changes to the Connection-State.. */
  13.  
  14.         private void setupForDataStateChanges() {
  15.  
  16.                 this.dsir = new DataStateIntentReceiver(this, new Handler(){
  17.  
  18.                         @Override
  19.  
  20.                         public void handleMessage(Message msg) {
  21.  
  22.                                 Log.d(Constants.DEBUGTAG, "DataStateIntentReceiver Msg received.");
  23.  
  24.  
  25.  
  26.                                 /* Determine what kind of message we are receiving, checking the what-id. */
  27.  
  28.                                 if(msg.what == IGPSMapActivity.CONNECTIONSTATE_WHAT_ID){
  29.  
  30.                                         DataState ds = IGPSMapActivity.this.dsir.getConnectionState();
  31.  
  32.                                         /* Switch on the ConnectionState... */
  33.  
  34.                                         switch(ds){
  35.  
  36.                                                 case CONNECTED:
  37.  
  38.                                                         //...
  39.  
  40.                                                         break;
  41.  
  42.                                                 case CONNECTING:
  43.  
  44.                                                         //...
  45.  
  46.                                                         break;
  47.  
  48.                                                 case DISCONNECTED:
  49.  
  50.                                                         //...
  51.  
  52.                                                         break;
  53.  
  54.                                         }
  55.  
  56.                                 }else if(msg.what == IGPSMapActivity.CONNECTIONACTIVITY_WHAT_ID){
  57.  
  58.                                         DataActivityState as = IGPSMapActivity.this.dsir.getActivityState();
  59.  
  60.                                         switch(as){
  61.  
  62.                                                 case DATAIN:
  63.  
  64.                                                         //...
  65.  
  66.                                                         break;
  67.  
  68.                                                 case DATAOUT:
  69.  
  70.                                                         //...
  71.  
  72.                                                         break;
  73.  
  74.                                                 case DATAINANDOUT:
  75.  
  76.                                                         //...
  77.  
  78.                                                         break;
  79.  
  80.                                                 case NONE:
  81.  
  82.                                                         //...
  83.  
  84.                                                         break;
  85.  
  86.                                         }
  87.  
  88.                                 super.handleMessage(msg);
  89.  
  90.                         }
  91.  
  92.                 });
  93.  
  94.                 // Finally tell the System, that we want to be notified of ConnectionState-Changes...
  95.  
  96.                 this.dsir.notifyConnectionState(CONNECTIONSTATE_WHAT_ID);
  97.  
  98.                 this.dsir.notifyActivity(CONNECTIONACTIVITY_WHAT_ID);
  99.  
  100.                 this.dsir.registerIntent();
  101.  
  102.         }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Maybe Eclipse doesn't make the imports automatically --> Here are they:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.telephony.DataStateIntentReceiver;
  2.  
  3. import android.telephony.Phone.DataState;
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

[align=center]Thats it :)[/align]

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby drake » Mon Mar 31, 2008 11:22 pm

Hey plusminus. Thanks so much for your prompt reply. I really appreciate all the help. However, I am still having problems implementing the datastateintentreceiver.

1) In my switch statement, I ALWAYS get a "CONNECTED" data state, even after I turn my internet off.

2) There doesn't seem to be any information on the class DataState. How did you know to use it?

Here is a snippet of my code:

GLOBAL VARIABLES:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static final int GETCONNECTION = 1;
  2.  
  3. protected DataStateIntentReceiver dsir;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


IN ONCREATE():

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. dsir = new DataStateIntentReceiver(this, connection_handler);
  2.  
  3. dsir.notifyConnectionState(GETCONNECTION);
  4.  
  5. dsir.registerIntent();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


SEPARATE HANDLER CREATED IN THE SAME ACTIVITY CLASS:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private Handler connection_handler = new Handler(){
  2.  
  3. @Override
  4.  
  5. public void handleMessage(Message msg) {
  6.  
  7.  
  8.  
  9. DataState ds = dsir.getConnectionState();
  10.  
  11. Log.i(TAG,"This is the value of connected = "+ds.toString());
  12.  
  13.  
  14.  
  15. /* check connection state */
  16.  
  17. switch(ds){
  18.  
  19. case CONNECTED:
  20.  
  21. Log.i(TAG, "I'M CONNECTED!");
  22.  
  23. break;
  24.  
  25. case CONNECTING:
  26.  
  27. break;
  28.  
  29. case DISCONNECTED:
  30.  
  31. Log.i(TAG, "I'M DISCONNECTED!!!");
  32.  
  33. break;
  34.  
  35. }
  36.  
  37. super.handleMessage(msg);
  38.  
  39. }
  40.  
  41. };
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Again, thanks in advanced.
drake
Freshman
Freshman
 
Posts: 4
Joined: Thu Feb 28, 2008 1:45 am

Postby plusminus » Thu Apr 03, 2008 7:22 am

Hello drake,

as the Internet Connection of the emulator is also tunneled throught your pc, the emulator probably doesn't recognize, when you disable your desktop connection.

But, vary the settings in Eclipse DDMS View (need Eclipse ADT Plugin for that).

That works with me and I think that is what we finally can rely on.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Zanoe » Fri Apr 11, 2008 11:09 am

Hello Plusminus,

Emh can you help me where, I can disabled/enabled the internet connection on DDMS?? I'm newbie here, and this code very usefull for me.

Thank's before! :lol: :lol:
Zanoe
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 26, 2007 10:47 am

Postby sappersix » Sun Apr 13, 2008 10:45 pm

Zanoe,
In Eclipse, go to Window-->Open Perspective-->Other.... Then choose DDMS. From here you can control the emulator.

PlusMinus,
I've been on the boards for about a month. Really appreciate the knowledge and assistance provided by you and the AndDev community. These boards are much more useful than the Google groups discussions.

Best,
s6
sappersix
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Feb 26, 2008 2:12 am

Postby Zanoe » Mon Apr 14, 2008 5:08 am

sappersix wrote:Zanoe,
In Eclipse, go to Window-->Open Perspective-->Other.... Then choose DDMS. From here you can control the emulator.

PlusMinus,
I've been on the boards for about a month. Really appreciate the knowledge and assistance provided by you and the AndDev community. These boards are much more useful than the Google groups discussions.

Best,
s6


Yepz you're right PlusMinus, this board is more usefull than Google groups discussion especially for newbie, and the developer who want to learn Android from Zero to Hero :lol: :lol: You can get all the thing you need here, like me most of my app code inspirated from this forum hehe, So thank you very much for all members and PlusMinus who build this board :lol: :lol:
Zanoe
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 26, 2007 10:47 am

Top

Postby androooooid » Fri Feb 20, 2009 6:29 pm

Has anyone got this to work with the new sdk?
androooooid
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Sep 26, 2008 8:47 pm

Postby MrSnowflake » Sat Feb 21, 2009 11:05 am

DataStateIntentReceiver isn't in the SDK anymore, maybe it's renamed?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 10 guests