版本更新

本文介绍了一个使用旋转动画触发新版本检查的应用案例。通过在启动应用时展示一个旋转动画,当动画播放结束后,程序会自动跳转到新页面进行版本信息的检查,并提示用户是否有新版本可用。

//动画结束跳转更新版本
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        ImageView img = (ImageView) findViewById(R.id.img);
        //设置动画
        RotateAnimation  animation = new RotateAnimation(0, 360*4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //设置动画时间
        animation.setDuration(2000);
        //保持动画结束时候的状态、
        animation.setFillAfter(true);
        img.setAnimation(animation);
        //设置动画的监听事件
        animation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                Intent  in = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(in);
            }
        });
    }
}




//检查版本信息
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Xml;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
    private int versionCode;
    private String path = "http://www.oschina.net/MobileAppVersion.xml";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        // 获得当前版本号
        try {
            versionCode = getVersionName();
            Log.e("TAG", "项目版本号:" + versionCode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 请求网络数据
        forInfo();
    }
    // 请求网络数据
    private void forInfo() {
        new AsyncTask<String, Void, InputStream>() {

            @Override
            protected InputStream doInBackground(String... params) {
                try {
                    URL url = new URL(path);

                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();

                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(3000);

                    // /判断结果码
                    if (conn.getResponseCode() == 200) {
                        InputStream inputStream = conn.getInputStream();

                        return inputStream;
                    }

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(InputStream result) {
                super.onPostExecute(result);
                // 解析xml文件
                try {
                    UpdataInfo info = getUpdataInfo(result);
                    Log.e("TAG", "info.vercode= "+info.versionCode);
                    // 对比版本号
                    if (info.versionCode.equals(versionCode+"")) {
                        Toast.makeText(Main2Activity.this, "已经是最新版本",
                                Toast.LENGTH_SHORT).show();
                    } else {

                        // 弹出提示框
                        tishigengxin(info);
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }.execute(path);

    }

    // 提示更新的框
    protected void tishigengxin(final UpdataInfo info) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("有新版本").setMessage(info.updateLog);

        builder.setNegativeButton("以后再说",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                });
        builder.setPositiveButton("立即更新",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        xiazaiapk(info.downloadUrl);// 下载
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();

    }

    // 下载APK
    protected void xiazaiapk(final String url) {
        final ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("正在下载新版本");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.show();

        new Thread() {
            public void run() {
                File file = getapk(pd, url);
                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file),
                        "application/vnd.android.package-archive");
                startActivity(intent);
                pd.dismiss();
            }
        }.start();

    }

    // 安装APK
    protected File getapk(final ProgressDialog pd, final String url) {

        try {
            URL url2 = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url2.openConnection();
            conn.setConnectTimeout(5000);
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                int contentLength = conn.getContentLength();
                pd.setMax(contentLength);
                InputStream inputStream = conn.getInputStream();
                File file = new File(Environment.getExternalStorageDirectory(),
                        "update.apk");
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                int len = 0;
                byte[] buffer = new byte[1024];
                int total = 0;
                while ((len = bis.read(buffer)) != -1) {

                    fileOutputStream.write(buffer, 0, len);
                    total += len;
                    pd.setProgress(total);
                }

                return file;
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    // 获得当前版本号
    private int getVersionName() throws Exception {
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(
                "com.bwie.test", 0);
        return packageInfo.versionCode;
    }

    // 获得服务器的版本号
    public static UpdataInfo getUpdataInfo(InputStream is) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "utf-8");// 设置解析的数据源
        int type = parser.getEventType();
        UpdataInfo info = new UpdataInfo();// 实体
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
                case XmlPullParser.START_TAG:
                    if ("versionCode".equals(parser.getName())) {
                        info.versionCode = parser.nextText(); // 获取版本号
                    } else if ("downloadUrl".equals(parser.getName())) {
                        info.downloadUrl = parser.nextText(); // 获取要升级的APK文件
                    } else if ("updateLog".equals(parser.getName())) {
                        info.updateLog = parser.nextText(); // 获取该文件的信息
                    }
                    break;
            }
            type = parser.next();
        }
        return info;
    }

}



//ben包
public class UpdataInfo {

    public String versionCode;
    public String versionName;
    public String downloadUrl;
    public String updateLog;
    public UpdataInfo() {
        super();
        // TODO Auto-generated constructor stub
    }
    public UpdataInfo(String versionCode, String versionName,
                      String downloadUrl, String updateLog) {
        super();
        this.versionCode = versionCode;
        this.versionName = versionName;
        this.downloadUrl = downloadUrl;
        this.updateLog = updateLog;
    }
}


                
跟网型逆变器小干扰稳定性分析与控制策略优化研究(Simulink仿真实现)内容概要:本文围绕跟网型逆变器的小干扰稳定性展开分析,重点研究其在电力系统中的动态响应特性及控制策略优化问题。通过构建基于Simulink的仿真模型,对逆变器在不同工况下的小信号稳定性进行建模与分析,识别系统可能存在的振荡风险,并提出相应的控制优化方法以提升系统稳定性和动态性能。研究内容涵盖数学建模、稳定性判据分析、控制器设计与参数优化,并结合仿真验证所提策略的有效性,为新能源并网系统的稳定运行提供理论支持和技术参考。; 适合人群:具备电力电子、自动控制或电力系统相关背景,熟悉Matlab/Simulink仿真工具,从事新能源并网、微电网或电力系统稳定性研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 分析跟网型逆变器在弱电网条件下的小干扰稳定性问题;② 设计并优化逆变器外环与内环控制器以提升系统阻尼特性;③ 利用Simulink搭建仿真模型验证理论分析与控制策略的有效性;④ 支持科研论文撰写、课题研究或工程项目中的稳定性评估与改进。; 阅读建议:建议读者结合文中提供的Simulink仿真模型,深入理解状态空间建模、特征值分析及控制器设计过程,重点关注控制参数变化对系统极点分布的影响,并通过动手仿真加深对小干扰稳定性机理的认识。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值