title: Running an app in background to copy a particular folder from pen drive to a physical device?
body: <p>I am creating an app which I want to run it in the background. The project has a basic concept of copying files from <strong><em>pen drive to android device</em></strong> in <strong>local storage</strong> without app opening and also to copy only a single folder named <strong>thinpc</strong> whenever pen drive is attached to a device it automatically starts copying folder (thinpc) without any permission. I have done the copying part but the issue is that the app is not working automatically I have to do touch that folder to copy which I don't want to do. 
  Here is the reference link to complete code and also I am sharing the MainActivity.java </p>
  
  <p>" <a href="https://github.com/magnusja/libaums" rel="nofollow noreferrer">https://github.com/magnusja/libaums</a> "</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>private void discoverDevice() {
      UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
      massStorageDevices = UsbMassStorageDevice.getMassStorageDevices(this);
  
      if (massStorageDevices.length == 0) {
          Log.w(TAG, "no device found!");
          android.support.v7.app.ActionBar actionBar = getSupportActionBar();
          actionBar.setTitle("No Pen drive detected ");
          listView.setAdapter(null);
          return;
      }else {
          android.support.v7.app.ActionBar actionBar = getSupportActionBar();
          actionBar.setTitle("thinpc");
      }
  
    @Override
  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long rowId) {
      UsbFile entry = adapter.getItem(position);
      try {
          if (entry.isDirectory()) {
              dirs.push(adapter.getCurrentDir());
              listView.setAdapter(adapter = new UsbFileListAdapter(this, entry));
          } else {
  
              if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                      != PackageManager.PERMISSION_GRANTED) {
  
                  if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                          Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                      Toast.makeText(this, R.string.request_write_storage_perm, Toast.LENGTH_LONG).show();
                  } else {
                      ActivityCompat.requestPermissions(this,
                              new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                              REQUEST_EXT_STORAGE_WRITE_PERM);
                  }
  
                  return;
              }
  
              CopyTaskParam param = new CopyTaskParam();
              param.from = entry;
              File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                      + "/thinpc/cache");
              f.mkdirs();
              int index = entry.getName().lastIndexOf(".") &gt; 0
                      ? entry.getName().lastIndexOf(".")
                      : entry.getName().length();
              String prefix = entry.getName().substring(0, index);
              String ext = entry.getName().substring(index);
              // prefix must be at least 3 characters
              if(prefix.length() &lt; 3) {
                  prefix += "pad";
              }
              param.to = File.createTempFile(prefix, ext, f);
              new CopyTask().execute(param);
          }
      } catch (IOException e) {
          Log.e(TAG, "error staring to copy!", e);
      }
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if (resultCode != Activity.RESULT_OK) {
          Log.w(TAG, "Activity result is not ok");
          return;
      }
  
      if (requestCode == OPEN_STORAGE_PROVIDER_RESULT) {
          Uri uri;
          if (data != null) {
              uri = data.getData();
              Log.i(TAG, "Uri: " + uri.toString());
              Intent i = new Intent(Intent.ACTION_VIEW);
              i.setData(uri);
              startActivity(i);
          }
      } else if (requestCode == COPY_STORAGE_PROVIDER_RESULT) {
          Uri uri;
          if (data != null) {
              uri = data.getData();
              Log.i(TAG, "Uri: " + uri.toString());
  
              CopyToUsbTaskParam params = new CopyToUsbTaskParam();
              params.from = uri;
  
              new CopyToUsbTask().execute(params);
          }
      } else if (requestCode == OPEN_DOCUMENT_TREE_RESULT) {
          Uri uri;
          if (data != null) {
              uri = data.getData();
              Log.i(TAG, "Uri: " + uri.toString());
  
              CopyToUsbTaskParam params = new CopyToUsbTaskParam();
              params.from = uri;
  
              new CopyFolderToUsbTask().execute(params);
          }
      }
  }
  </code></pre>
  
  <p>Remaining code is in the reference link above
  Thanks in advance</p>