public boolean isWifiOnline(FragmentActivity activity) { // network 연결 상태 확인
try {
ConnectivityManager conMan = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo.State wifi = conMan.getNetworkInfo(1).getState(); // wifi
if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
return true;
}
} catch (NullPointerException e) {
return false;
}
return false;
}
2016년 5월 17일 화요일
android wifi ap 연결 예제 샘플 소스
public void connectWifi(WifiItem menuItem) {
/////////////////// 공통 //////////////////////////
WifiConfiguration wfc = new WifiConfiguration();
wfc.SSID = "\"".concat(menuItem.getSsid()).concat("\"");
wfc.status = WifiConfiguration.Status.DISABLED;
wfc.priority = 40;
/////////////////// 공통 //////////////////////////
// name: jimmyNet
// pw : 12345678
////////////////// 보안 방식에 따른 설정 //////////////////////
int type = 2;
if (type == 0) {
// open일때
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedAuthAlgorithms.clear();
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
} else if (type == 1) {
// wep일때
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wfc.wepKeys[0] = "12345678"; // <<--이부분중요. \"부분 빼고 그냥 비밀번호만 넣는다
wfc.wepTxKeyIndex = 0;
} else if (type == 2) {
// wpa나 wpa2일때
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wfc.preSharedKey = "\"".concat("12345678").concat("\"");
}
////////////////// 보안 방식에 따른 설정 //////////////////////
int networkId = wifimanager.addNetwork(wfc);
if (networkId != -1) {
wifimanager.enableNetwork(networkId, true);
}
}
/////////////////// 공통 //////////////////////////
WifiConfiguration wfc = new WifiConfiguration();
wfc.SSID = "\"".concat(menuItem.getSsid()).concat("\"");
wfc.status = WifiConfiguration.Status.DISABLED;
wfc.priority = 40;
/////////////////// 공통 //////////////////////////
// name: jimmyNet
// pw : 12345678
////////////////// 보안 방식에 따른 설정 //////////////////////
int type = 2;
if (type == 0) {
// open일때
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedAuthAlgorithms.clear();
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
} else if (type == 1) {
// wep일때
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wfc.wepKeys[0] = "12345678"; // <<--이부분중요. \"부분 빼고 그냥 비밀번호만 넣는다
wfc.wepTxKeyIndex = 0;
} else if (type == 2) {
// wpa나 wpa2일때
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wfc.preSharedKey = "\"".concat("12345678").concat("\"");
}
////////////////// 보안 방식에 따른 설정 //////////////////////
int networkId = wifimanager.addNetwork(wfc);
if (networkId != -1) {
wifimanager.enableNetwork(networkId, true);
}
}
android Exceptions are not yet supported across processes. 해결
Exceptions are not yet supported across processes. 에러가 났다.
UI 쓰레드에서 처리해야 하는 문제로 판단하여 UI thread를 실행 시켜서 해결됨.
getActivity().runOnUiThread(() -> {
Toast.makeText(getActivity(), "bool=" + bool, Toast.LENGTH_SHORT).show();
mMenuItemAdapter.setMenu(null);
});
UI 쓰레드에서 처리해야 하는 문제로 판단하여 UI thread를 실행 시켜서 해결됨.
getActivity().runOnUiThread(() -> {
Toast.makeText(getActivity(), "bool=" + bool, Toast.LENGTH_SHORT).show();
mMenuItemAdapter.setMenu(null);
});
android EditText의 텍스트(string) 값 가져오기
EditText editText = (EditText)findViewById(R.id.editText);
editText.getText.toString();을 하면 String객체로 Text를 리턴하게 된다.
공백 체크는 아래와 같이
if ( editText.getText.toString().length() == 0 ) {
//공백일 때 처리할 내용
} else {
//공백이 아닐 때 처리할 내용
}
editText.getText.toString();을 하면 String객체로 Text를 리턴하게 된다.
공백 체크는 아래와 같이
if ( editText.getText.toString().length() == 0 ) {
//공백일 때 처리할 내용
} else {
//공백이 아닐 때 처리할 내용
}
android SharedPreferences 사용 방법
선언
SharedPreferences mPref = PreferenceManager.getDefaultSharedPreferences(context);
데이터 저장
SharedPreferences.Editor editor = mPref.edit();
editor.putString("key", "value");
editor.commit();
데이터 호출
String callValue = mPref.getString("key", "default value");
*호출하는 키에 대응하는 값이 없을 경우 디폴트로 설정한 데이터가 출력된다.
데이터 삭제
SharedPreferences.Editor editor = mPref.edit();
editor.remove("key"); or editor.clear();
editor.commit();
SharedPreferences mPref = PreferenceManager.getDefaultSharedPreferences(context);
데이터 저장
SharedPreferences.Editor editor = mPref.edit();
editor.putString("key", "value");
editor.commit();
데이터 호출
String callValue = mPref.getString("key", "default value");
*호출하는 키에 대응하는 값이 없을 경우 디폴트로 설정한 데이터가 출력된다.
데이터 삭제
SharedPreferences.Editor editor = mPref.edit();
editor.remove("key"); or editor.clear();
editor.commit();
android DialogFragment 배경 색 없에기, 배경색 설정
DialogFragment의 View뒤에 배경색(검은색) 없에기
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
DialogFragment의 View뒤에 배경색 주기
getDialog().getWindow().setDimAmount(0.3F); // 뒷 배경 30% dim 처리
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
DialogFragment의 View뒤에 배경색 주기
getDialog().getWindow().setDimAmount(0.3F); // 뒷 배경 30% dim 처리
피드 구독하기:
글 (Atom)