Android對APK文件的安裝和卸載

安裝APK文件

方法一:傳入File文件

    public void installApp(File file) {
        try {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(android.content.Intent.ACTION_VIEW);
            String type = "application/vnd.android.package-archive";
            intent.setDataAndType(Uri.fromFile(file), type);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

方法二:傳入文件名稱

    public void installApp(String file_name) {
        try {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(android.content.Intent.ACTION_VIEW);
            String type = "application/vnd.android.package-archive";
            intent.setDataAndType(Uri.parse(file_name), type);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



卸載APK

    Uri uri = Uri.parse("package:com.xxx.xxx");   
    Intent intent = new Intent(Intent.ACTION_DELETE, uri);   
    startActivity(intent);
Uri解析的內容必須爲“package:包名”,利用ACTION_DELETE動作和Uri數據,就可以卸載了。


在安裝和卸載時,並不需要另外在AndroidManifest.xml文件添加權限。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章