Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- RadioButton off = (RadioButton) findViewById(R.id.radio_off);
- off.toggle();
- }
- private void setStream(int id) {
- final String hosts[] = {
- "http://192.168.1.21:8080/",
- "http://192.168.1.22:8088/"
- };
- final String files[][] = {
- { "?off","?cz","?en","?de","?pl" },
- { "?off","?cz","?en","?de","?pl" }
- };
- for(int i=0; i<hosts.length; i++) {
- GetController gc = new GetController(hosts[i]+files[i][id]);
- Thread t = new Thread(gc);
- t.start();
- }
- }
- public void onRadioButtonClicked(View view) {
- ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
- if (networkInfo != null && networkInfo.isConnected()) {
- boolean checked = ((RadioButton) view).isChecked();
- if(checked) {
- switch(view.getId()) {
- case R.id.radio_off:
- setStream(0);
- break;
- case R.id.radio_cz:
- setStream(1);
- break;
- case R.id.radio_en:
- setStream(2);
- break;
- case R.id.radio_de:
- setStream(3);
- break;
- case R.id.radio_pl:
- setStream(4);
- break;
- }
- }
- } else {
- Toast.makeText(getApplicationContext(),"No connection...",Toast.LENGTH_SHORT).show();
- }
- }
- private class GetController implements Runnable {
- private String myurl;
- public GetController(String url) {
- myurl = url;
- }
- public void run() {
- android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
- try {
- URL url = new URL(myurl);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(1000 /* milliseconds */);
- conn.setConnectTimeout(1000 /* milliseconds */);
- conn.setRequestMethod("GET");
- conn.setDoInput(true);
- conn.connect();
- int response = conn.getResponseCode();
- InputStream is = conn.getInputStream();
- Reader reader = new InputStreamReader(is, "UTF-8");
- char[] buffer = new char[500];
- reader.read(buffer);
- conn.disconnect();
- } catch(IOException e) {
- } catch(MalformedURLException e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment