//ComponentName comp = new ComponentName(context.getPackageName(),MyService.class.getName());
// ComponentName service = context.startService(new Intent().setComponent(comp));
public class MyService extends Service {
private FileObserver observer;
private static final String pathToWatch = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
private static final String TAG = "FileObserverActivity";
String Save_Path;
String Save_folder = "/test/";
private int count=0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
String ext = Environment.getExternalStorageState();
if(ext.equals(Environment.MEDIA_MOUNTED)){
Save_Path=Environment.getExternalStorageDirectory().getAbsolutePath() + Save_folder;
}
Toast.makeText(getBaseContext(), "서비스 시작", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Notification notification = new Notification(R.drawable.ic_launcher, "모니터 서비스 실행됨", System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), "APK Monitor", "실시간 모니터링중...", null);
startForeground(1, notification);
observer = new FileObserver(pathToWatch) {
@Override
public void onEvent(int event, String path) {
//128 : 다운로드 완료시 , usb로 저장시
//32 : 디렉토리 오픈시
//512 : 파일삭제
Log.d(TAG, "File Changed --> Path = " + pathToWatch +"/"+ path);
Log.d(TAG, "File Changed --> Event = " + event);
if(event==128){
Log.d(TAG,"야호" + path);
File dir = new File(Save_Path);
if(!dir.exists()){
dir.mkdir();
Log.d(TAG,"디렉토리 생성");
}
}
}
};
observer.startWatching();
}
}