Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Android Background processing How to download something in the background in an Android application?
This code fetches content from the web without blocking the UI (runs in the background in a Thread). Once finished, it posts a Handler that is picked up by the UI as soon as possible.
import import import import import java.io.BufferedInputStream; java.io.InputStream; java.net.URL; java.net.URLConnection; org.apache.http.util.ByteArrayBuffer;
private Handler mHandler; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mHandler = new Handler(); checkUpdate.start(); } private Thread checkUpdate = new Thread() { public void run() { try { URL updateURL = new URL("http://iconic.4feets.com/update"); URLConnection conn = updateURL.openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while((current = bis.read()) != -1){ baf.append((byte)current); } /* Convert the Bytes read to a String. */ html = new String(baf.toByteArray()); mHandler.post(showUpdate); } catch (Exception e) { } } }; private Runnable showUpdate = new Runnable(){ public void run(){ Toast.makeText(Iconic.this, "HTML Code: " + html, Toast.LENGTH_SHORT).show(); } }; }
@Override protected void onStop(){ super.onStop(); // Save user preferences. We need an Editor object to // make changes. All objects are from android.context.Context SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Don't forget to commit your edits!!! editor.commit(); } }
Android Hardware detection How to programmatically figure out the screen resolution?
This code will give you the screen resolution at run time. The size of your application may differ due to the status and title bar.
@Override public void onCreate(Bundle icicle) { WindowManager w = getWindowManager(); Display d = w.getDefaultDisplay(); int width = d.getWidth(); int height = d.getHeight(); }
"Message", true); final Handler handler = new Handler() { public void handleMessage(Message msg) { dialog.dismiss(); } }; Thread checkUpdate = new Thread() { public void run() { // // YOUR LONG CALCULATION (OR OTHER) GOES HERE // handler.sendEmptyMessage(0); } }; checkUpdate.start();
and method two, you want to specify a client (NoteToMe here), replace the intent request by:
NoteToMe.this.startActivity(emailIntent); NoteToMe.this.finish();
This function will return true if the network is available, false if it is not (airplane mode, out of reach, etc.). IMPORTANT : Don't forget to add [HTML_REMOVED] in your manifest, or the line NetworkInfo[] info = connectivity.getAllNetworkInfo(); will crash !
public boolean isNetworkAvailable() { Context context = getApplicationContext(); ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { boitealerte(this.getString(R.string.alert),"getSystemService rend null"); } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; }
this example has been provided by iznogoud from androidsnippets Network|How to execute a HTTP GET Request with HttpClient? Retrieve the contents of a stream via a HTTP GET.
public static InputStream getInputStreamFromUrl(String url) { InputStream content = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(url)); content = response.getEntity().getContent(); } catch (Exception e) { Log.("[GET REQUEST]", "Network exception", e); } return content; }