note

Progressbar로 로딩표현 (인터넷로딩) 본문

Android/기본

Progressbar로 로딩표현 (인터넷로딩)

투한 2012. 1. 31. 22:35


maifest.xml에서 권한설정을 해줍니다
(uses permission)


실행화면


실행화면

무언가를 읽어들이는 것에는 무조건 Progress가 뜹니다



 


javafile
package kr.android.web.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ProgressBarDemo1 extends Activity {
	private WebView web;
	private ProgressBar progress;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		boolean isWifiConn = ni.isConnected();

		ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		boolean isMobileConn = ni.isConnected();

		if (!isWifiConn && !isMobileConn) {
			Toast.makeText(this, "인터넷에 접속되어 있지 않습니다!", Toast.LENGTH_SHORT)
			.show();
			finish();//액티비티 종료
		} else {
			setContentView(R.layout.main);

			progress = (ProgressBar) findViewById(R.id.web_progress);

			web = (WebView) findViewById(R.id.web);
			web.getSettings().setJavaScriptEnabled(true);
			web.getSettings().setBuiltInZoomControls(true);
			web.setHorizontalScrollbarOverlay(true);
			web.setVerticalScrollbarOverlay(true);

			web.loadUrl("http://raysoda.com");

			web.setWebViewClient(new WebViewClient() {
				// 링크 클릭에 대한 반응
				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					view.loadUrl(url);
					return true;
				}

				// 웹페이지 호출시 오류 발생에 대한 처리
				@Override
				public void onReceivedError(WebView view, int errorcode,
						String description, String fallingUrl) {
					Toast.makeText(ProgressBarDemo1.this,
							"오류 : " + description, Toast.LENGTH_SHORT).show();
				}
				// 페이지 로딩 시작시 호출
				@Override
				public void onPageStarted(WebView view,String url , Bitmap favicon){
					progress.setVisibility(View.VISIBLE);
				}
				//페이지 로딩 종료시 호출
				public void onPageFinished(WebView view,String Url){
					progress.setVisibility(View.GONE);
				}
			});
		}
	}
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if(keyCode ==KeyEvent.KEYCODE_BACK && web.canGoBack()){
			web.goBack();
		}else if(keyCode ==KeyEvent.KEYCODE_BACK && !web.canGoBack()){
			Toast.makeText(this, "프로그램 종료!!", Toast.LENGTH_SHORT).show();
			finish();
		}
		return true;
	}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <WebView
            android:id="@+id/web"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <ProgressBar
            android:id="@+id/web_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" />
    </FrameLayout>

</LinearLayout>



두번째 타이틀에 로딩바심기(ProgressBar)


실행화면




이건 인터넷 권한만 있으면 됩니다

 


 

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/web"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


javafile
package kr.android.web.progressbar2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class ProgressBarDemo2 extends Activity {
	private WebView web;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 타이틀바에 막대모양 프로세스 setContentView();
		// 호출 전에 호출 해야함
		getWindow().requestFeature(Window.FEATURE_PROGRESS);

		setContentView(R.layout.main);

		web = (WebView) findViewById(R.id.web);
		web.getSettings().setJavaScriptEnabled(true);
		web.getSettings().setBuiltInZoomControls(true);

		web.loadUrl("http://m.naver.com");

		final Activity activity = this;
		web.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				// progress 구간 0~10000
				activity.setProgress(progress * 100);
				Log.i("progressBarDemo2", "Progress :" + progress);
			}
		});
		web.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String fallingUrl) {
				Toast.makeText(activity, "오류 : " + description,
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
	}
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if(keyCode ==KeyEvent.KEYCODE_BACK && web.canGoBack()){
			web.goBack();
		}else if(keyCode ==KeyEvent.KEYCODE_BACK && !web.canGoBack()){
			Toast.makeText(this, "프로그램 종료!!", Toast.LENGTH_SHORT).show();
			finish();
		}
		return true;
	}
}