권한 설정을 해줘야함


적용이 되었습니다.



<?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" >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        
    <TextView
        android:id="@+id/instruct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press Button for Action" />
    
    <Button
        android:id="@+id/do_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go!" />
        
    </LinearLayout>
    <TextView
        android:id="@+id/status"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
package kr.android.network;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NetWorkStatusDemo extends Activity {
	TextView status;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        status = (TextView)findViewById(R.id.status);
        
        Button goButton = (Button)findViewById(R.id.do_action);
        
        goButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				ConnectivityManager cm = 
						(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
				//Wifi 정보 호출
				NetworkInfo ni =cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
				
				//사용 가능 여부
				boolean isWifiAvail = ni.isAvailable();
				
				//접속 여부
				boolean isWifiConn = ni.isConnected();
				
				//모바일(3G) 정보 호출
				ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
				
				//사용 가능 여부
				boolean isMobileAvail = ni.isAvailable();
				
				//접속 여부
				boolean isMobileConn = ni.isConnected();
				
				status.setText("Wifi\n연결가능 =" +isWifiAvail+"\n연결 = " 
				+ isWifiConn+"\nMobile\n연결 가능 ="+isMobileAvail+"\n 연결 ="+isMobileConn);
			}
		});
    }
}



'Android > 기본' 카테고리의 다른 글

Read Write File (자동 저장할때 사용)  (0) 2012.04.28
Access Web  (0) 2012.04.28
Browser WebView (브라우저)  (0) 2012.04.28
Handler 사용하기 (로딩 표시?)  (0) 2012.04.28
Activity 생명주기  (0) 2012.04.28

+ Recent posts