51,407
社区成员
发帖
与我相关
我的任务
分享
package com.srtw.wx.app;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.ProgressDialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.content.Context;
import java.net.URL;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.os.Message;
import android.util.Log;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Environment;
import android.widget.Toast;
import java.io.IOException;
import com.lblll.yulingkeji.yxdapp.R;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
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.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class SplashActivity extends Activity {
private static final int GO_HOME = 1000;
private final int UPDATA_NONEED = 0;
private final int UPDATA_CLIENT = 1;
private final int GET_UNDATAINFO_ERROR = 2;
private final int SDCARD_NOMOUNTED = 3;
private final int DOWN_ERROR = 4;
boolean isFirstIn = true;
private static final String SHAREDPREFERENCES_NAME = "first_pref";
private static final String SHAREDPREFERENCES2_NAME = "my_pref";
private final static int SWITCH_MAINACTIVITY = 1000;
private final static int SWITCH_GUIDACTIVITY = 1001;
/**
* Handler:跳转到不同界面
*/
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case GO_HOME:
goHome();
break;
case UPDATA_CLIENT:
//对话框通知用户升级程序
downLoadApk();
break;
case DOWN_ERROR:
//下载apk失败
Toast.makeText(getApplicationContext(), "下载新版本失败", Toast.LENGTH_SHORT).show();
break;
}
super.handleMessage(msg);
switch (msg.what) {
case SWITCH_MAINACTIVITY:
Intent mIntent = new Intent();
mIntent.setClass(SplashActivity.this, MyActivity.class);
SplashActivity.this.startActivity(mIntent);
SplashActivity.this.finish();
break;
case SWITCH_GUIDACTIVITY:
mIntent = new Intent();
mIntent.setClass(SplashActivity.this, GuideActivity.class);
SplashActivity.this.startActivity(mIntent);
SplashActivity.this.finish();
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
setContentView(R.layout.activity_splash);
boolean mFirst = isFirstEnter(SplashActivity.this, SplashActivity.this.getClass().getName());
if (mFirst)
mHandler.sendEmptyMessageDelayed(SWITCH_GUIDACTIVITY, 5000);
else
mHandler.sendEmptyMessageDelayed(SWITCH_MAINACTIVITY, 5000);
//检查网络
if (chkNetwork()) {
CheckAppVersion();
} else {
mHandler.sendEmptyMessageDelayed(GO_HOME, 3000);
}
}
private void isFirstEnter(SplashActivity splashActivity, String name) {
// 读取SharedPreferences中需要的数据
// 使用SharedPreferences来记录程序的使用次数
SharedPreferences preferences = getSharedPreferences(SHAREDPREFERENCES_NAME, MODE_PRIVATE);
// 取得相应的值,如果没有该值,说明还未写入,用true作为默认值
isFirstIn = preferences.getBoolean("isFirstIn", true);
// 判断程序与第几次运行,如果是第一次运行则跳转到引导界面,否则跳转到主界面
if (!isFirstIn) {
// 使用Handler的postDelayed方法,3秒后执行跳转到MainActivity
mHandler.sendEmptyMessageDelayed(GO_HOME, 5000);
} else {
//mHandler.sendEmptyMessageDelayed(GO_GUIDE, 3000);
}
}
private void init() {
// 读取SharedPreferences中需要的数据
// 使用SharedPreferences来记录程序的使用次数
SharedPreferences preferences = getSharedPreferences(SHAREDPREFERENCES_NAME, MODE_PRIVATE);
// 取得相应的值,如果没有该值,说明还未写入,用true作为默认值
isFirstIn = preferences.getBoolean("isFirstIn", true);
// 判断程序与第几次运行,如果是第一次运行则跳转到引导界面,否则跳转到主界面
if (!isFirstIn) {
// 使用Handler的postDelayed方法,3秒后执行跳转到MainActivity
mHandler.sendEmptyMessageDelayed(GO_HOME, 5000);
} else {
//mHandler.sendEmptyMessageDelayed(GO_GUIDE, 3000);
}
}
private void goHome() {
Intent intent = new Intent(SplashActivity.this, MyActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
//检查网络
public boolean chkNetwork() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
public void CheckAppVersion() {
try {
getLocalVerInfo();
Thread_getUpdVerInfo chkVer = new Thread_getUpdVerInfo();
chkVer.start();
chkVer.join();
checkVersion();
} catch (Exception e) {
//tvMsg.setText(e.getMessage());
}
}
public void getLocalVerInfo() {
try {
UpdateGlobal.localVerCode = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
UpdateGlobal.localVerName = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
} catch (Exception e) {
//tvMsg.setText(e.getMessage());
}
}
/**
* 检查更新版本
*/
public void checkVersion() {
//Toast.makeText(getApplicationContext(), UpdateGlobal.localVerCode+";"+ UpdateGlobal.serverVerCode, Toast.LENGTH_SHORT).show();
TextView tvVersionsShow = (TextView) findViewById(R.id.tvVersionsShow);
tvVersionsShow.append("" + UpdateGlobal.localVerName);
if (UpdateGlobal.localVerCode < UpdateGlobal.serverVerCode) {
//发现新版本,提示用户更新
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("软件升级")
.setMessage("发现新版本 " + UpdateGlobal.serverVerName + ",建议立即更新使用.")
.setPositiveButton("更新", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//开启更新服务UpdateService
//这里为了把update更好模块化,可以传一些updateService依赖的值
//如布局ID,资源ID,动态获取的标题,这里以app_name为例
//Intent updateIntent =new Intent(SplashActivity.this, UpdateService.class);
//updateIntent.putExtra("titleId",R.string.app_name);
//startService(updateIntent);
Message msg = new Message();
msg.what = UPDATA_CLIENT;
mHandler.sendMessage(msg);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mHandler.sendEmptyMessageDelayed(GO_HOME, 3000);
}
});
alert.create().show();
} else {
//清理工作,略去
//cheanUpdateFile(),文章后面我会附上代码
mHandler.sendEmptyMessageDelayed(GO_HOME, 3000);
}
}
//从服务器中下载APK
protected void downLoadApk() {
final ProgressDialog pd; //进度条对话框
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
pd.show();
new Thread() {
@Override
public void run() {
try {
File file = DownLoadManager.getFileFromServer(UpdateGlobal.serverDownUrl, pd);
sleep(3000);
installApk(file);
pd.dismiss(); //结束掉进度条对话框
} catch (Exception e) {
Message msg = new Message();
msg.what = DOWN_ERROR;
mHandler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
//安装apk
protected void installApk(File file) {
Intent intent = new Intent();
//执行动作
intent.setAction(Intent.ACTION_VIEW);
//执行的数据类型
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
//根据传过来url创建文件
private File getFile(String url) {
File files = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), getFilePath(url));
return files;
}
//截取出url后面的apk的文件名
private String getFilePath(String url) {
return url.substring(url.lastIndexOf("/"), url.length());
}
}
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_splash);
//
// boolean mFirst = isFirstEnter(SplashActivity.this,SplashActivity.this.getClass().getName());
// if(mFirst)
// mHandler.sendEmptyMessageDelayed(SWITCH_GUIDACTIVITY,5000);
// else
// mHandler.sendEmptyMessageDelayed(SWITCH_MAINACTIVITY,5000);
// }
//****************************************************************
// 判断应用是否初次加载,读取SharedPreferences中的guide_activity字段
//****************************************************************
// private static final String SHAREDPREFERENCES_NAME = "my_pref";
// private static final String KEY_GUIDE_ACTIVITY = "guide_activity";
// private boolean isFirstEnter(Context context,String className){
// if(context==null || className==null||"".equalsIgnoreCase(className))return false;
// String mResultStr = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_WORLD_READABLE)
// .getString(KEY_GUIDE_ACTIVITY, "");//取得所有类名 如 com.my.MainActivity
// if(mResultStr.equalsIgnoreCase("false"))
// return false;
// else
// return true;
// }
//*************************************************
// Handler:跳转至不同页面
//*************************************************
// private final static int SWITCH_MAINACTIVITY = 1000;
// private final static int SWITCH_GUIDACTIVITY = 1001;
// public Handler mHandler = new Handler(){
// public void handleMessage(Message msg) {
// switch(msg.what){
// case SWITCH_MAINACTIVITY:
// Intent mIntent = new Intent();
// mIntent.setClass(SplashActivity.this, MainActivity.class);
// SplashActivity.this.startActivity(mIntent);
// SplashActivity.this.finish();
// break;
// case SWITCH_GUIDACTIVITY:
// mIntent = new Intent();
// mIntent.setClass(SplashActivity.this, GuideActivity.class);
// SplashActivity.this.startActivity(mIntent);
// SplashActivity.this.finish();
// break;
// }
// super.handleMessage(msg);
// }