Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

AsyncTask Example Android with ProgressBar

MainActivity.java

package com.asynctaskexample;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView textView;
	private ProgressBar progressBar;
	private DownloadWebPageTask mTask = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView) findViewById(R.id.TextView01);
		downloadPage();
	}

	// AsyncTask <TypeOfVarArgParams , ProgressValue , ResultValue> .
	private class DownloadWebPageTask extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			//textView.setText("Hello !!!");
			progressBar = (ProgressBar) findViewById(R.id.progressBar1);
			progressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);

		}

		@Override
		protected String doInBackground(String... urls) {
			String response = "";
			for (String url : urls) {
				DefaultHttpClient client = new DefaultHttpClient();
				HttpGet httpGet = new HttpGet(url);
				try {
					HttpResponse execute = client.execute(httpGet);
					InputStream content = execute.getEntity().getContent();

					BufferedReader buffer = new BufferedReader(new InputStreamReader(
							content));
					String s = "";
					while ((s = buffer.readLine()) != null) {
						response += s;
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return response;
		}

		@Override
		protected void onPostExecute(String result) {
			progressBar.setVisibility(View.INVISIBLE);
			textView.setText(result);
		}
	}

	private void downloadPage() {
		if (mTask != null
				&& mTask.getStatus() != DownloadWebPageTask.Status.FINISHED) {
			mTask.cancel(true);
		}
               // execute(String[]) you can put array of links to web pages, or array of Integer[] 
               // if first param is Integer[] etc.
		mTask = (DownloadWebPageTask) new DownloadWebPageTask()
				.execute(new String[] { "//android.okhelp.cz/android-market.html",
						"//android.okhelp.cz/android-market.html" });
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (mTask != null
				&& mTask.getStatus() != DownloadWebPageTask.Status.FINISHED) {
			mTask.cancel(true);
			mTask = null;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}




activity_main.xml

<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>



AndroidManifest.xml do not forget INTERNET uses-permission !!!!!!!

<uses-permission android:name="android.permission.INTERNET" />


396LW NO topic_id




AD

Další témata ....(Topics)


349

fragment Binary XML file line Must specify unique android:id, android:tag, or have a parent with an id for | fragment-binary-xml-file-line-must-specify-unique-androidid-androidtag-or-have-a-parent-with-an-id-for


FrameLayou, fragment have to unique id android:id="@+id/your_id_unique"

<FrameLayout xmlns:android="//schemas.android.com/apk/res/android"
    xmlns:tools="//schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/your_id_unique"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="cz.okhelp.notepad.AddNoteFragment"
        tools:layout="@layout/add_note" />

</FrameLayout>
351

How to test dual panels - panes fragments on small device screen Android | how-to-test-dual-panels-fragments-on-small-device-screen-android


If You have old PC (Android in emulator with high screen resolution uses a lot of memory) or your testing phone have small screen, You can try this trick.
Rename layout folder for small device screen f.g. from layout into layout-swXXXdp and a large layout-sw600dp into layout.
Your phone with small screnn will do select xml file from renamed layout folder (for small screen).
You can to testing rotation with device en stability of fragments if an application changed orientation.
If you have old pc, you can try to make a new virtual device running on older version of Android, which uses less memory of computer.
For testing of rotation stability - application orientation changed - use on emulator Ctrl+F11, Ctrl+F12 key (Windows).

Important
Before the release of application, you have to rename layout folder to older name layout-sw600dp and layout-swXXXdp to layout.
222

Disable enable internet connection in Android Emulator | disable-enable-internet-connection-in-android-emulator


If you try function for checking internet connection you can disable internet on the emulator:
Settings - Wireless and networks - Mobile networks - Data enabled (checked - unchecked )


 public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) 
          getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }