天天看點

擷取Android應用的初次安裝時間、版本名、版本号

<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Consolas';font-size:12.0pt;"><span style="color:#cc7832;">public </span>String <span style="color:#ffc66d;">getVersion</span>() {
    <span style="color:#cc7832;">try </span>{
        PackageManager manager = <span style="color:#cc7832;">this</span>.getPackageManager()<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">        </span>PackageInfo <span style="background-color:#40332b;">info</span> = manager.getPackageInfo(<span style="color:#cc7832;">this</span>.getPackageName()<span style="color:#cc7832;">, </span><span style="color:#6897bb;">0</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">        </span><span style="color:#808080;">//</span><span style="color:#808080;font-family:'宋體';">擷取應用第一次安裝的時間
</span><span style="color:#808080;font-family:'宋體';">        </span><span style="color:#9876aa;">first </span>= <span style="background-color:#344134;">info</span>.<span style="color:#9876aa;">firstInstallTime</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">        </span><span style="color:#808080;">//</span><span style="color:#808080;font-family:'宋體';">擷取應用的版本号
</span><span style="color:#808080;font-family:'宋體';">        </span><span style="color:#9876aa;">versionCode </span>= <span style="background-color:#344134;">info</span>.<span style="color:#9876aa;">versionCode</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">        </span><span style="color:#9876aa;">version </span>= <span style="background-color:#344134;">info</span>.<span style="color:#9876aa;">versionName</span><span style="color:#cc7832;">;</span><span style="color:#808080;">//</span><span style="color:#808080;font-family:'宋體';">擷取應用的版本名
</span><span style="color:#808080;font-family:'宋體';">        </span>System.<span style="color:#9876aa;"><em>out</em></span>.println(<span style="color:#6a8759;">"first:" </span>+ <span style="color:#9876aa;">first</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">        </span>System.<span style="color:#9876aa;"><em>out</em></span>.println(<span style="color:#6a8759;">"versionCode:" </span>+ <span style="color:#9876aa;">versionCode</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">        </span>System.<span style="color:#9876aa;"><em>out</em></span>.println(<span style="color:#6a8759;">"version:" </span>+ <span style="color:#9876aa;">version</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">    </span>} <span style="color:#cc7832;">catch </span>(Exception e) {
        e.printStackTrace()<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">    </span>}
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">version</span><span style="color:#cc7832;">;
</span>}
           

//擷取Android應用的初次安裝時間、版本名、版本号

public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            //擷取應用第一次安裝的時間
            first = info.firstInstallTime;
            //擷取應用的版本号
            versionCode = info.versionCode;
            version = info.versionName;//擷取應用的版本名
            System.out.println("first:" + first);
            System.out.println("versionCode:" + versionCode);
            System.out.println("version:" + version);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return version;
    }
           

繼續閱讀