每次打开应用时检查更新

public class Test extends Activity {
    private Handler mHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.front);
        mHandler = new Handler();
        
        /** 获取最后一次更新时间并保存到 Preferences 中 **/
        SharedPreferences prefs = getPreferences(0);
        lastUpdateTime =  prefs.getLong("lastUpdateTime", 0);
        
        /** 查检是否更新 **/
        if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {

            /** 保存时间以便下次更新**/
            lastUpdateTime = System.currentTimeMillis();            
            SharedPreferences.Editor editor = getPreferences(0).edit();
            editor.putLong("lastUpdateTime", lastUpdateTime);
            editor.commit();        

            /** 开始更新 **/            
            checkUpdate.start();
        }
    }
    
    /** 该线程在后台检查更新 **/
    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);
                }

                /** 读取的字节转换为字符串. **/ final String s = new String(baf.toByteArray());         
                
                /** 获取当前版本号 **/
                int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
                int newVersion = Integer.valueOf(s);
                
                /** 对比版本号? **/
                if (newVersion > curVersion) {
                    /** 显示更新 **/
                    mHandler.post(showUpdate);
                }                
            } catch (Exception e) {
            }
        }
    };

    /** 该线打开市场下载应用 **/ 
    private Runnable showUpdate = new Runnable(){
           public void run(){
            new AlertDialog.Builder(Test.this)
            .setIcon(R.drawable.icon)
            .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();
           }
    };    
}



转载于:https://my.oschina.net/oppo4545/blog/195224

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值