我是靠谱客的博主 自由蜜蜂,这篇文章主要介绍Android u盘安装第三方应用,现在分享给大家,希望可以做个参考。

u盘下载好第三方应用,识别后升级安装

public class UpdataAppActivity extends AppCompatActivity {

    private static final String TAG = "[DADAO]UpdataAppActivity";
    public static final String JUMP_UPDATA_APP = "jump_updata_app";
    public static final String CLOSE_UPDATA_APP_ACTIVITY = "close_updata_app_activity";
    private RecyclerView rv;
    private ProgressBar loading;

    private boolean isNowCopy = false;

    private UpdataAppHandler handler;

    /**
     * true成功
     * false失败
     */
    public static String COPY_RESULT_KEY = "COPY_RESULT_KEY";
    public static String COPY_SUCCESS_PATH_KEY = "COPY_SUCCESS_PATH_KEY";

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            UpdataAppActivity.this.finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata_app);

        loading = findViewById(R.id.loading);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CLOSE_UPDATA_APP_ACTIVITY);
        registerReceiver(receiver, intentFilter);

        Intent intent = getIntent();
        File[] files = (File[]) intent.getSerializableExtra(JUMP_UPDATA_APP);

        handler = new UpdataAppHandler(this);

        if (null != files && files.length > 0) {
            initRv(files);
        } else {
            finish();
        }
    }

    private void initRv(File[] files) {
        rv = findViewById(R.id.rv);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(manager);
        UpdataAppAdapter adapter = new UpdataAppAdapter(this, this::copyFile, files);
        rv.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != receiver) {
            unregisterReceiver(receiver);
        }
        if (null != handler) {
            handler.removeCallbacksAndMessages(null);
        }
        deleteApk();
    }

    public void resetStatus() {
        loading.setVisibility(View.GONE);
        isNowCopy = false;
    }

    private static class UpdataAppHandler extends Handler {

        private WeakReference<UpdataAppActivity> mWeakReference;

        public UpdataAppHandler(UpdataAppActivity activity) {
            this.mWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            Bundle bundle = msg.getData();
            boolean copyResult = bundle.getBoolean(COPY_RESULT_KEY, false);
            String path = bundle.getString(COPY_SUCCESS_PATH_KEY, "");
            UpdataAppActivity appActivity = mWeakReference.get();
            if (null != appActivity) {
                appActivity.resetStatus();
                if (copyResult && path.length() > 0) {
                    File apkFile = new File(path);
                    appActivity.installApk(apkFile);
                }
            }
        }
    }

    private void copyFile(File uApkFile) {
        if (null == uApkFile || isNowCopy) {
            return;
        }
        loading.setVisibility(View.VISIBLE);
        reallyCopy(uApkFile);
    }

    private void reallyCopy(File uApkFile) {
        isNowCopy = true;
        Thread thread = new Thread(() -> {
            boolean copySuccess = false;
            String path = "";
            try {
                //1.将u盘文件复制到内部
                File file = UpdataAppActivity.this.getFilesDir();
                Log.d(TAG, "getFilesDir(): " + file.getAbsolutePath());
                file.mkdir();
                File appFile = new File(file + "/app");
                appFile.mkdir();

                FileInputStream input = new FileInputStream(uApkFile);
                FileOutputStream output = new FileOutputStream(appFile + "/" + uApkFile.getName());

                //定义存储空间
                byte[] b = new byte[1024 * 5];
                //开始读文件
                int len;
                while ((len = input.read(b)) != -1) {
                    //将b中的数据写入到FileOutputStream对象中
                    output.write(b, 0, len);
                }

                output.flush();
                output.close();
                input.close();
                copySuccess = true;
                Log.d(TAG, "Copy succeeded");

                path = appFile.getAbsolutePath() + "/" + uApkFile.getName();

            } catch (Exception e) {
                Log.d(TAG, "Copy fail");
                Log.d(TAG, e.toString());
                e.printStackTrace();
            }

            if (null != handler) {
                Message message = Message.obtain();
                Bundle bundle = new Bundle();
                bundle.putBoolean(COPY_RESULT_KEY, copySuccess);
                bundle.putString(COPY_SUCCESS_PATH_KEY, path);
                message.setData(bundle);
                Log.d(TAG, "reallyCopy sendMessage");
                handler.sendMessage(message);
            }
        });
        thread.start();
    }

    private void installApk(File file) {

        Log.d(TAG, "installApk");
        Log.d(TAG, "apk file path: " + file.getAbsolutePath());

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_GRANT_READ_URI_PERMISSION |
                Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

        // minSdkVersion 26
        Uri uri = FileProvider.getUriForFile(this, "com.android.settings.files", file);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        try {
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, e.toString());
        }
    }

    public void deleteApk() {
        File filesDir = this.getFilesDir();
        File file = new File(filesDir.getAbsolutePath() + "/app/");
        Log.d(TAG, "deleteDirWihtFile getFilesDir: " + file.getAbsolutePath());
        if (file.exists()) {
            for (File appFile : file.listFiles()) {
                if (appFile.isFile())
                    appFile.delete();
            }
        }
    }
}

最后

以上就是自由蜜蜂最近收集整理的关于Android u盘安装第三方应用的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部