Overview:
Check whether the program is up-to-date every day through the backgroundVersion
To update the currentVersion
In the displayed dialog box, select whether to update the software in the current market.
Knowledge points:
- Sharedpreferences: a lightweight storage method, similar to a frequently used. ini file, it also retrieves corresponding values by retrieving keywords. The reason why it becomes lightweight is that it can apply only a limited number of numeric types and has a relatively low efficiency for storing large numeric values. Official reference
- System. currenttimemillis: represents the current time in milliseconds and is used to compare the order of two times. (The value indicates the total number of milliseconds from 00:00:00 till the current time) official reference
- Read information through the network: The checkupdate () method contains a URL to read network resources. For detailed operation steps, see source code.
- Runnable: implements any code to be executed in its internal run () method. When this runnable interface is called, it can be regarded as a new thread.
Source code:
- Public class Hello extends activity {
- /** Called when the activity is first created .*/
- Private handler mhandler;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Mhandler = new handler ();
- /* Get last update time from preferences */
- Sharedpreferences prefs = getpreferences (0 );
- Long lastupdatetime = prefs. getlong ("lastupdatetime", system. currenttimemillis ());
- Int curversion = 0;
- Try {
- Curversion = getpackagemanager (). getpackageinfo ("linhai.com. Hello", 0). versioncode;
- } Catch (namenotfoundexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- Log. I ("Demo", String. valueof (curversion ));
- /* Shocould activity check for updates now? */
- If (lastupdatetime + (24x60*60*1000) <system. currenttimemillis ()){
- /* Save current timestamp for next check */
- Lastupdatetime = system. currenttimemillis ();
- Sharedpreferences. Editor editor = getpreferences (0). Edit ();
- Editor. putlong ("lastupdatetime", lastupdatetime );
- Editor. Commit ();
- /* Start update */
- // Checkupdate. Start ();
- }
- }
- /* This thread checks for updates in the background */
- Private thread checkupdate = new thread ()
- {
- Public void run (){
- Try {
- URL updateurl = new URL ("http://my.company.com/update ");
- Urlconnection conn = updateurl. openconnection ();
- Inputstream is = conn. getinputstream ();
- Bufferedinputstream Bis = new bufferedinputstream (is );
- Bytearraybuffer BAF = new bytearraybuffer (50 );
- Int current = 0;
- While (current = bis. Read ())! =-1 ){
- BAF. append (byte) Current );
- }
- /* Convert the bytes read to a string .*/
- Final string S = new string (BAF. tobytearray ());
- /* Get current version number */
- Int curversion = getpackagemanager (). getpackageinfo ("Your. App. ID", 0). versioncode;
- Int newversion = integer. valueof (s );
- /* Is a higher version than the current already out? */
- If (newversion> curversion ){
- /* Post a handler for the UI to pick up and open the dialog */
- Mhandler. Post (showupdate );
- }
- } Catch (exception e ){
- }
- }
- };
- /* This runnable creates a dialog and asks the user to open the market */
- Private runnable showupdate = new runnable (){
- Public void run (){
- New alertdialog. Builder (hello. This)
- . Seticon (R. drawable. OK)
- . Settitle ("update available ")
- . Setmessage ("an update for is available! /N/nopen Android Market and see the details? ")
- . Setpositivebutton ("yes", new dialoginterface. onclicklistener (){
- Public void onclick (dialoginterface dialog, int whichbutton ){
- /* User clicked OK so do some stuff */
- Intent intent = new intent (intent. action_view, Uri. parse ("Market: // search? Q = pname: Your. App. ID "));
- Startactivity (intent );
- }
- })
- . Setnegativebutton ("no", new dialoginterface. onclicklistener (){
- Public void onclick (dialoginterface dialog, int whichbutton ){
- /* User clicked cancel */
- }
- })
- . Show ();
- }
- };
- }
Copy code
There are three parts:
- Programs placed in the oncreate () method are used to determine whether update needs to be checked for the current time (if the last update time is greater than one day, check for updates will be started)
- When the preceding conditions are met, start checkupdate to check whether the current program is up to date.Version
.
- If you are sureVersion
If it has expired, you will log on to the market and direct it to the current program page.
**************************************** **************************************** ***********
Up:
I have posted a help post on this issue in the forum. Although the idea is similar to the above, the key point is how to update the program, now we can see that the update method mentioned here is actually logging on to the market. However, all the programs released later are in the market, and the problem does not exist.
- Intent intent = new intent (intent. action_view, Uri. parse ("Market: // search? Q = pname: Your. App. ID "));
- Startactivity (intent );
Copy code
Everyone is developing it on Eclipse. Do you notice the console prompt every time you update the code and run the simulator:
- [19:53:50-Hello] Android launch!
- [19:53:50-Hello] ADB is running normally.
- [19:53:50-Hello] Ming linhai.com. Hello. Hello activity launch
- [19:53:50-Hello] Automatic Target mode: using existing emulator 'emulator-5554 'running compatible avd'
- [19:53:50-Hello] Warning: application does not specify an API level requirement!
- [19:53:50-Hello] device API version is 3 (Android 1.5)
- [5554 19:53:50-Hello] uploading hello.apk onto device 'emulator-100'
- [19:53:50-Hello] installing hello.apk...
- [19:54:05-Hello] application already exists. Attempting to re-install instead...
- [19:54:31-Hello] success!
Copy code
Analysis:
1. Android running properly
2. Run our program in the configuration file androidmanifest. xml.
3. Uploading hello.apk onto device 'emulator-100'
This is the key. update our program.
4. Installing hello.apk...
5. Application already exists. Attempting to re-install instead...
// The program already exists. Try to reinstall it.
So if our program needs to be updated automatically, I guess it is the same as the above steps.
For details, see the log in logcat.
- 06-06 11:54:02. 567: Debug/packageparser (582): scanning package:/data/APP/vmdl12464.tmp
- 06-06 11:54:08. 048: INFO/packagemanager (582): removing non-system package: linhai.com. Hello
- 06-06 11:54:08. 187: Debug/packagemanager (582): removing package linhai.com. Hello
- 06-06 11:54:08. 286: Debug/packagemanager (582): Activities: linhai.com. Hello. Hello
- 06-06 11:54:11. 136: Debug/packagemanager (582): scanning package linhai.com. Hello
- 06-06 11:54:11. 301: INFO/packagemanager (582):/data/APP/vmdl12464.tmp changed; unpacking
- 06-06 11:54:11. 626: Debug/installd (555): dexinv: --- begin '/data/APP/vmdl12464.tmp '---
- 06-06 11:54:12. 987: Debug/dalvikvm (7756): dexopt: Load 224 ms, verify 265 ms, OPT 1 ms
- 06-06 11:54:13. 047: Debug/installd (555): dexinv: --- end '/data/APP/vmdl12464.tmp' (SUCCESS )---
- 06-06 11:54:13. 057: Debug/packagemanager (582): Activities: linhai.com. Hello. Hello
- 06-06 11:54:15. 608: INFO/installd (555): Move
/Data/Dalvik-Cache/Data @ app@vmdl12464.tmp @ classes. Dex->
/Data/Dalvik-Cache/data@app@linhai.com.hello.apk @ classes. Dex
- 06-06 11:54:15. 737: Debug/packagemanager (582): new package installed in/data/APP/linhai.com.hello.apk
Copy code
The third-party management software for such Automatic Updates has been calledAtrackdog
The principle is to use the above method.
About getVersion
Number, use:
- Int curversion = getpackagemanager (). getpackageinfo ("Your. App. ID", 0). versioncode;
Copy code
ProgramVersion
In the androidmanifest. xml file:
- <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
- Package = "linhai.com. Hello" Android: versioncode = "2" Android: versionname = "2.0.1">
Copy code
The main point is about getpackagemanager (). There are many methods in this section. You can use it to obtain the programs installed on the current terminal. The Installation Package function is getpackagemanager (). installpackage (packageuri)