"Android" to determine the "New installation first opened, the first open after upgrade, the second open," such as can be used to determine whether the "Boot page",!

Source: Internet
Author: User

Ideas:

1. Based on sharedpreferences, each time you open, you can distinguish this open situation according to the version recorded on the last Open.



Storeddata.java:

1.application.oncreate call Storeddata.getthis (). Markopenapp (); Other locations can be opened according to the Getlaunchmode judgment open type.

Package Com.example.test;import Android.app.application;import Android.content.sharedpreferences;import Android.content.pm.packageinfo;import Android.content.pm.packagemanager;import Android.text.TextUtils;public Class Storeddata {public static final int lmode_new_install = 1;//start-mode, first install-first boot, overwrite installation-first boot, installed-two start public static final  int lmode_update = 2;public static final int lmode_again = 3;private Boolean isopenmarked = false;private int launchmode = Lmode_again; Start-mode private static Storeddata instance;private sharedpreferences share; General information public static Storeddata Getthis () {if (instance = = null) instance = new Storeddata (); return instance;} -------Boot status------------------------------------------------------------//mark-open app to generate-whether to open public void for the first time Markopenapp () {//Prevent-repeat call if (isopenmarked) return;isopenmarked = true; String lastversion = share.getstring ("Lastversion", "" "); String thisversion = Getappversion ();//First start if (Textutils.isempty (lastversion)) {Launchmode = Lmode_new_instalL;share.edit (). putstring ("Lastversion", thisversion). commit ();} Update else if (!thisversion.equals (lastversion)) {Launchmode = Lmode_update;share.edit (). putstring ("Lastversion", thisversion). commit ();} Two start (version unchanged) Elselaunchmode = Lmode_again;} public int Getlaunchmode () {return launchmode;} First open, new installation, overwrite (different version number) public Boolean isfirstopen () {return launchmode! = Lmode_again;} -------------------------//Software-version public static string Getappversion () {String versionname = ""; Application app = Myapplication.getthis (); try {Packagemanager pkgmng = App.getpackagemanager (); PackageInfo pkgInfo = Pkgmng.getpackageinfo (App.getpackagename (), 0); versionname = Pkginfo.versionname;} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} return versionname;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.