Guest User

prasarna.java

a guest
Jul 12th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. public class MainActivity extends Activity
  2. {
  3.     @Override
  4.     public void onCreate(Bundle savedInstanceState)
  5.     {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.main);
  8.         RadioButton off = (RadioButton) findViewById(R.id.radio_off);
  9.         off.toggle();
  10.     }
  11.  
  12.     private void setStream(int id) {
  13.         final String hosts[] = {
  14.             "http://192.168.1.21:8080/",
  15.             "http://192.168.1.22:8088/"
  16.         };
  17.         final String files[][] = {
  18.             { "?off","?cz","?en","?de","?pl" },
  19.             { "?off","?cz","?en","?de","?pl" }
  20.         };
  21.         for(int i=0; i<hosts.length; i++) {
  22.             GetController gc = new GetController(hosts[i]+files[i][id]);
  23.             Thread t = new Thread(gc);
  24.             t.start();
  25.         }
  26.     }
  27.  
  28.     public void onRadioButtonClicked(View view) {
  29.         ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  30.         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
  31.         if (networkInfo != null && networkInfo.isConnected()) {
  32.             boolean checked = ((RadioButton) view).isChecked();
  33.             if(checked) {
  34.                 switch(view.getId()) {
  35.                     case R.id.radio_off:
  36.                         setStream(0);
  37.                         break;
  38.                     case R.id.radio_cz:
  39.                         setStream(1);
  40.                         break;
  41.                     case R.id.radio_en:
  42.                         setStream(2);
  43.                         break;
  44.                     case R.id.radio_de:
  45.                         setStream(3);
  46.                         break;
  47.                     case R.id.radio_pl:
  48.                         setStream(4);
  49.                         break;
  50.                 }
  51.             }
  52.         } else {
  53.             Toast.makeText(getApplicationContext(),"No connection...",Toast.LENGTH_SHORT).show();
  54.         }
  55.     }
  56.  
  57.     private class GetController implements Runnable {
  58.         private String myurl;
  59.  
  60.         public GetController(String url) {
  61.             myurl = url;
  62.         }
  63.  
  64.         public void run() {
  65.             android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
  66.             try {
  67.                 URL url = new URL(myurl);
  68.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  69.                 conn.setReadTimeout(1000 /* milliseconds */);
  70.                 conn.setConnectTimeout(1000 /* milliseconds */);
  71.                 conn.setRequestMethod("GET");
  72.                 conn.setDoInput(true);
  73.                 conn.connect();
  74.                 int response = conn.getResponseCode();
  75.                 InputStream is = conn.getInputStream();
  76.                 Reader reader = new InputStreamReader(is, "UTF-8");        
  77.                 char[] buffer = new char[500];
  78.                 reader.read(buffer);
  79.                 conn.disconnect();
  80.             } catch(IOException e) {
  81.             } catch(MalformedURLException e) {
  82.             }
  83.         }
  84.  
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment