본문 바로가기

android studio11

Android 앱 캐시 및 데이터 내가 원하는것만 골라서 삭제해보자. public static void clearApplicationData(Context context) { File cache = context.getCacheDir(); File appDir = new File(cache.getParent()); if (appDir.exists()) { String[] children = appDir.list(); for (String list : children) { Log.d("캐시삭제 ", list); if(list.equals("shared_prefs")) continue; deleteDir(new File(appDir, list)); } } } private static boolean deleteDir(File dir) { if (dir != null && di.. 2021. 8. 20.
Android (타이머) 시간마다 실행하기 (주기별 실행) Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { @Override public void run() { //5초마다 실행 } }; timer.schedule(timerTask,0,5000); ms단위로 적어주시면 됩니다. 2021. 7. 2.
Android polling(폴링) 백그라운드에서 API통신 해보기. 안드로이드에서 백그라운드딴에서 앱이 종료되도 주기적으로 서버와 통신하며 상태를 확인해보는 작업을 해볼게요. 프로젝트에서 오른쪽 클릭해서 new - Service - Service 해서 서비스를 상속받는 클래스를 만들어주세요 (안드로이드 4대 컴포넌트!!) public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } } 이게 초기상태로 작성된 서비스.. 2021. 7. 2.
안드로이드 스튜디오 현재 기기의 배터리 잔량 파악하기. public int getBatteryState(Context context) { Intent batteryStatus = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale; return (int)(batteryPct * 100); } 해당 함수를 정의하고 인자(파라미터)값에 Context를.. 2021. 6. 24.