CS185c
Chris Pollett
Mar 12, 2012
public void onCreate(Bundle savedInstanceState) { String message = ""; super.onCreate(savedInstanceState); setContentView(R.layout.main); try { ConnectivityManager cMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); if(cMgr != null) { NetworkInfo netInfo = cMgr.getActiveNetworkInfo(); message = "View1 "+ netInfo.toString(); Log.i("NetInfoTest", message); } } catch(Exception e) { message = "That didn't work: "+e.toString(); Log.i("NetInfoTest", message); } // example using toast Context context = getApplicationContext(); int duration = Toast.LENGTH_LONG; Toast toast = Toast.makeText(context, message, duration); toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); toast.show(); //setting text field's content TextView netinfoText = (TextView)this.findViewById(R.id.net_info); netinfoText.setText(message); Button button = (Button)this.findViewById(R.id.switch_view); button.setOnClickListener(this); } public void onClick(View v) { Intent i = new Intent(this, NetInfo2.class); startActivity(i); }
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> The first is for general networking, the second is for the actual info we have above.
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // only will trigger it if no physical keyboard is open mgr.showSoftInput(netinfoText, InputMethodManager.SHOW_IMPLICIT);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(netinfoText.getWindowToken(), 0);
import java.io.*; import java.net.*; public final class MyServer extends Thread { private static final int PORT=8889; private static final int NUM_CONNECT=1; private MyServer() {} public static void main(String args[]) { MyServer myServer = new MyServer(); if(myServer !=null) {myServer.start();} } public void run() { try { ServerSocket server = new ServerSocket(PORT, NUM_CONNECT); Socket client = server.accept(); BufferedReader reader = new BufferedReader( new InputStreamReader(client.getInputStream())); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(client.getOutputStream())); // do some reading and writing } catch(IOException ie) { ie.printStackTrace(); } } }
Which of the following is true?
package org.pollett; import android.app.Activity; import android.os.Bundle; import android.net.*; import android.content.*; import android.util.*; public class MyClient extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.socketButton.setOnClickListener(new OnClickListener)() { public void onClick(final View v) { socketOutput.setText(""); String output = callSocket(ipAddress.getText().toString(), port.getText().toString(), socketInput.getText().toString()); socketOutput.setText(output); } } } private String callSocket(String ip, String port, String socketData) { Socket socket = null; BufferedWriter writer = null; BufferedReader reader =null; String output = null; try{ socket = new Socket(ip, Integer.parseInt(port)); reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); writer = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())); writer.write("hello world"+socketData+"\n"); writer.flush(); output = reader.readLine(); } catch(Exception e){} return output; } }
<uses-permission android:name="android.permission.INTERNET"></uses-permission>to the manifest file
<WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="wrap_content" />
webview = (WebView) findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://www.pollett.org/");
private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }
webview.setWebViewClient(new MyWebViewClient());
URL/service_name/method_name/?arg1=value1&arg2=value2...which requests from the given URL the given method of the given web service with the given arguments.
String location="URL/service_name/method_name/?arg1=value1&arg2=value2"; String result = null; try { URL url = new URL(location); } catch (MalformedURLException e) { //handle somehow } if(url != null) { try { HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( urlConn.getInputStream())); String inputLine; while((inputLine = in.readLine()) != null) { result += "\n" + inputLine; } } catch (IOException e) { // handle somehow } return result; } return null;