在后台运行应用程序以将特定文件夹从笔式驱动器复制到物理设备?

时间:2018-05-01 06:47:40

标签: java android

我正在创建一个应用程序,我想在后台运行它。该项目的基本概念是在本地存储中将文件从 笔式驱动器复制到Android设备 而不打开应用程序,并且只复制单个文件夹名为 thinpc 只要笔式驱动器连接到设备,它就会自动开始复制文件夹(thinpc)而不经过任何许可。我已经完成了复制部分,但问题是该应用程序无法自动工作我必须触摸该文件夹才能复制我不想做的事情。 这是完成代码的参考链接,我也在分享MainActivity.java

" https://github.com/magnusja/libaums"

MainActivity.java

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<?> 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(".") > 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() < 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);
        }
    }
}

剩余代码位于上面的参考链接中 提前致谢

0 个答案:

没有答案