Changing values of "Settext" upon verifying an internet connection


Last Updated:

  1. evenstevens

    evenstevens New Member This Topic's Starter

    Joined:
    Mar 2, 2010
    Messages:
    1
    Likes Received:
    0
    So I have a Splashscreen at the start of my program, and I want it to display whether or not an active connection has been made to the internet upon this screen. Else the program should exit or display some sort of warning.

    However, I realise I need some sort of handler to change the text from "Checking for an internet connection..." to something like "Connection found!", but I have no idea what KIND of handler, as all the ones I know of are UI based.

    Any ideas?

    Just using connecinfo.setText("blah blah"); just crashes the program, as expected...


    Cheers!

    Code (Text):
    1.  
    2. public class Dam extends Activity {
    3.     protected boolean active = true;
    4.     protected int splashTime = 3000;
    5.    
    6.     /** Called when the activity is first created. */
    7.     @Override
    8.     public void onCreate(Bundle savedInstanceState) {
    9.         super.onCreate(savedInstanceState);
    10.         setContentView(R.layout.splash);
    11.        
    12.  
    13.         //Thread for making a connection and verifying it
    14.  
    15.         Thread connectivity = new Thread()
    16.         {
    17.             public void run()
    18.             {
    19.                 try{
    20.                     //################################################
    21.                     //############# CONNECTIVITY STUFF ###################
    22.                     //################################################
    23.  
    24.                     TextView connecinfo = (TextView)findViewById(R.id.connectioninfo);
    25.            
    26.                     ConnectivityManager connec =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    27.                    
    28.                     connecinfo.setText("Checking for an internet connection...");
    29.                    
    30.                     if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
    31.                         connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING )
    32.                     {
    33. < SHOULD SAY "INTERNET FOUND!" >
    34.                     }
    35.            
    36.                     else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||
    37.                              connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED)
    38.                     {
    39. < SHOULD SAY "NO INTERNET FOUND!" >
    40.                     }  
    41.                    
    42.  
    43.                 }
    44.                 catch(Exception e1){}
    45.             }
    46.         };
    47.         connectivity.start();
    48.  
    49.                     //################################################
    50.                     //################################################
    51.                     //################################################
    52.  
    53.        
    54.         // Thread for displaying the SplashScreen
    55.  
    56.         Thread splashThread = new Thread()
    57.         {
    58.             @Override
    59.             public void run() {
    60.                 try
    61.                 {
    62.                     int waited = 0;
    63.  
    64.                     while(active && (waited < splashTime)) {
    65.                         sleep(100);
    66.                         waited += 100;  
    67.                     }
    68.                    
    69.                 }
    70.                 catch(InterruptedException e) {}
    71.                     // do nothing
    72.                  finally {
    73.                     finish();
    74.                     startActivity(new Intent("com.beavers.dam.menu"));
    75.                     stop();
    76.                 }
    77.             }
    78.         };
    79.         splashThread.start();
    80.     }
    81.    
    82.     @Override
    83.     public boolean onTouchEvent(MotionEvent event) {
    84.         if (event.getAction() == MotionEvent.ACTION_DOWN) {
    85.             active = false;
    86.         }
    87.         return true;
    88.     }
    89. }
     

    Advertisement

Share This Page

Loading...