0

I have already scanned all of the related questons&answers here, but I still couldn't find the solution.

the service class file:

public class otser extends Service {

private WindowManager windowManager;
private ImageView chatHead;

@Override public IBinder onBind(Intent intent) {

    return null;
}

@Override public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);

    chatHead.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {

        }
    });

    FileObserver observer = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots") {
        @Override
        public void onEvent(int event, String file) {
            event &= FileObserver.ALL_EVENTS;

            if(event == FileObserver.CREATE){
                Toast.makeText(getApplicationContext(), "screenshot taken",
                        Toast.LENGTH_LONG).show();
            }
        }
    };

    observer.startWatching(); // start the observer


    Toast.makeText(getApplicationContext(), "service: OK",
            Toast.LENGTH_SHORT).show();

}}

According to other posts, I double checked the path, via creating a file programatically, it does exist, and correct.

The Activity class:

public class Home extends Activity {

Button showChatHead;
Button stopService;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);


    showChatHead = (Button) findViewById(R.id.gomb);
    stopService= (Button) findViewById(R.id.gomb2);

    showChatHead.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent i = new Intent(getApplicationContext(), ChatHeadService.class);
            startService(i);
        }
    });

   stopService.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent i = new Intent(getApplicationContext(), ChatHeadService.class);
            stopService(i);
        }
    });
}}

The service class linked well, the last Toast message pops up, as a conclusion only the observer is being skipped.

Any ideas?

Thanks in advance!


newer release:

FileObserver fileObserver = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots") {
        @Override
        public void onEvent(int event, String path) {

            Toast.makeText(getApplicationContext(), "method entered", Toast.LENGTH_SHORT).show();

            if (event == FileObserver.CREATE) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "File created", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    };

    fileObserver.startWatching();

The onEvent method is not entered, the "method entered" Toast didn't appear.

1 Answers1

0

in documentation of the onEvent-Method is mentioned, that it will run at another thread and you should consider this. using a handler, the Toast message should appear, like this:

private Handler handler = new Handler();

@Override
public void onCreate() {
    fileObserver = new FileObserver("path") {
        @Override
        public void onEvent(int event, String path) {
            if (event == FileObserver.CREATE) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(FileWatcher.this, "File created", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    };
    fileObserver.startWatching();
Lemao1981
  • 2,225
  • 5
  • 18
  • 30