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";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(getBaseContext(), "서비스 시작", Toast.LENGTH_LONG).show();
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);
String Save_folder = "/service_test/";
String Save_Path=Environment.getExternalStorageDirectory().getAbsolutePath() + Save_folder;
File dir = new File(Save_Path);
if(!dir.exists()){
dir.mkdir();
}
if(event==128){
Log.d(TAG,"야호" + path);
}
}
};
observer.startWatching();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
// foreground noti
Notification notification = new Notification(R.drawable.ic_launcher, "서비스 실행됨", System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), "Screen Service", "Foreground로 실행됨", null);
startForeground(1, notification);
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy(){
super.onDestroy();
}
}