<uses-permission android:name=
"android.permission.INTERNET"
>
<uses-permission android:name=
"android.permission.ACCESS_NETWORK_STATE"
>
------------------------------------------------------------
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
public boolean isOnlineToWifi(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if( netInfo != null && netInfo.isConnectedOrConnecting() ){
return true;
}
return false;
}
public boolean isOnlineToMobile(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( netInfo != null && netInfo.isConnectedOrConnecting() ){
return true;
}
return false;
}
-------------------------------------------------------------
<receiver android:name =".TestReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
public
class
clsNetwork
extends
BroadcastReceiver {
String action;
@Override
public
void
onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
action = intent.getAction();
if
(action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
{
//네트워크 변화가 생길경우에 처리할 코드 삽입
}
}
}
-------------------------------------
public class ConnectionBroadcastReceiver extends BroadcastReceiver{ private static final int STATE_NONE = 0; private static final int STATE_WIFI_CONNECTED = 1; private static final int STATE_MOBILE_CONNECTED = 2; private int state = STATE_NONE;
@Override public void onReceive(Context context, Intent intent) { try { String action = intent.getAction(); if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo niWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo niMobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (state == STATE_NONE) { if (niWifi.getState() == State.CONNECTED) { state = STATE_WIFI_CONNECTED; } else if (niMobile.getState() == State.CONNECTED) { state = STATE_MOBILE_CONNECTED; } } else if (state == STATE_WIFI_CONNECTED) { if (niWifi.getState() == State.DISCONNECTED || niWifi.getState() == State.DISCONNECTING) { state = STATE_NONE; Log.d("CBR", "Wi-Fi 끊어졌다!"); } } else if (state == STATE_MOBILE_CONNECTED) { if (niMobile.getState() == State.DISCONNECTED || niMobile.getState() == State.DISCONNECTING) { state = STATE_NONE; Log.d("CBR", "3G/4G 끊어졌다!"); } } } } catch (Exception e) { e.printStackTrace(); } }}
----------------------------------------------------------------
java.lang.SecurityException: ConnectivityService: Neither user 10290 nor current process has android.permission.ACCESS_NETWORK_STATE.
'android code' 카테고리의 다른 글
[Android] ListView에서 클릭이 제대로 인식되지 않는 문제 (3) | 2016.05.31 |
---|---|
안드로이드에서 파일 복사하기 (0) | 2016.03.07 |
Android SQLite Database Tutorial (0) | 2015.02.23 |
StrictMode.setClassInstanceLimit (0) | 2014.10.07 |
PendingIntent (0) | 2014.10.02 |