Quantcast
Channel: IT社区推荐资讯 - ITIndex.net
Viewing all articles
Browse latest Browse all 11804

安卓系统已安装软件的判断及通过程序安装

$
0
0

1. 查看系统是否已经安装某个应用

private boolean appInstalledOrNot(String uri) {
    PackageManager pm = getPackageManager();
    boolean appInstalled;
    try {
        pm.getPackageInfo(uri, 0);
        appInstalled = true;
    }
    catch (PackageManager.NameNotFoundException e) {
        appInstalled = false;
    }
    return appInstalled;
}

 

 uri是指AndroidManifest.xml文件中的package,是安卓应用的标识符。

 

2. 判断安装的版本号

private int getAPKVersion(String uri) {
    Integer version = -1;

    PackageManager packageManager=getPackageManager();
    try {
    	//0 means all the flags are turned off
        PackageInfo Info=packageManager.getPackageInfo(uri, 0);

        //like 5
        version = Info.versionCode;

        //like 1.0.5
        versionName = Info.versionName;

    } catch (PackageManager.NameNotFoundException e) {
        Log.e("error", e.getMessage());
    }

    return version;
}

 

 

3. 安装

基本思路:apk放到assets目录下,安装时先写到sd,再调用intent安装

private void installScannerPlugin() {
    AssetManager assetManager = getAssets();

    InputStream in;
    OutputStream out;

    try {
        in = assetManager.open("BarcodeScanner.apk");
        out = new FileOutputStream(Environment.getExternalStorageDirectory()
                + File.separator + "BarcodeScanner.apk");

        byte[] buffer = new byte[1024];

        int len;
        while((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }

        in.close();

        out.flush();
        out.close();

        Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory()
                        + File.separator + "BarcodeScanner.apk")),"application/vnd.android.package-archive");

        startActivity(intent);

    } catch(Exception e) {
        e.printStackTrace();
    }
}

 需要权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 



已有 0人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐




Viewing all articles
Browse latest Browse all 11804

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>