标签:
package com.bwie.activity;
import java.io.File;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.bwie.view.NetWorkHelper;
import com.bwie.vo.DownLoadManager;
import com.bwie.vo.Up_Data;
import com.bwie.vo.Versions;
import com.example.yunifa_veryg.R;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.util.Xml;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* 吕仙涛
* 主页 实现四个模块的切换
* @author Administrator
* @date 2016-7-7
*/
public class HomeActivity extends FragmentActivity implements OnClickListener{
private Button home_but_home;
private Button home_but_sort;
private Button home_but_cart;
private Button home_but_user;
private Fragment[] allfrag;
private FragmentManager manger;
private FragmentTransaction trans;
private TextView home_but_home_tv;
private TextView home_but_sort_tv;
private TextView home_but_cart_tv;
private TextView home_but_user_tv;
private Context context;
private TextView home_city_name;
private long mExitTime;
private Up_Data list_ver;
private String apkUrl = "http://m.yunifang.com/yunifang/mobile/version?random=67603&encode=e441af3089cbeeb6b58ac72393a66667&clientType=android&clientVersion=10000";
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
// Handler handler = new Handler(){
// @Override
// public void handleMessage(Message msg) {
// String xml = (String) msg.obj;
// vutils.jiexiApkData(xml);
// }
// };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home_activity_index);
mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类
mLocationClient.registerLocationListener( myListener ); //注册监听函数
//找控件
init();
//百度定位
initLocation();
//加监听
Listener();
//fragment事务管理切换
fragmanager();
//网络判断
internet();
//更新
// vutils();
jiexiApkData();
}
private void vutils() {
// TODO Auto-generated method stub
// vutils = new VersionUpdateUtils();
// vutils.reguestAPKData(HomeActivity.this);
}
public void jiexiApkData() {
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET,apkUrl, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
Gson g = new Gson();
Versions json = g.fromJson(arg0.result, Versions.class);
System.out.println(arg0.result);
list_ver = json.getData();
//String version = list_ver.get(0).getVersion_Name();
System.out.println("==========="+list_ver.getVersion_Name());
// 判断更新
panduanUpdate();
}
});
}
private void panduanUpdate() {
// TODO Auto-generated method stub
try {
PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
String version = info.versionName;
System.out.println("版本号"+version+list_ver.getVersion_Name());
if (version!=list_ver.getVersion_Name()) {
System.out.println("========="+list_ver.getVersion_Name());
initAlert();
}else{
Toast.makeText(getApplicationContext(), "要更新了", 0).show();
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initAlert() {
try {
Toast.makeText(getApplicationContext(), "更新给糖吃", 0).show();
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setTitle("软件版本更新");
builder.setMessage(list_ver.getDescription());
builder.setPositiveButton("立即更新",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 点击立即下载更新包
downLoadApk();
}
});
builder.setNegativeButton("以后再说",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void downLoadApk() {
final ProgressDialog pd; // 进度条对话框
pd = new ProgressDialog(context);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
pd.show();
// 进行下载操作
new Thread() {
@Override
public void run() {
try {
// 下载
File file = DownLoadManager.getFileFromServer(
list_ver.getUrl(), pd);
sleep(3000);
// 安装
installApk(file);
pd.dismiss(); // 结束掉进度条对话框
} catch (Exception e) {
}
}
}.start();
}
protected void installApk(File file) {
Intent intent = new Intent();
// 执行动作
intent.setAction(Intent.ACTION_VIEW);
// 执行的数据类型
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
}
//两次返回
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
Object mHelperUtils;
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
} else {
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private void internet() {
// TODO Auto-generated method stub
try {
if(NetWorkHelper.checkNetState(getApplicationContext())){
Toast.makeText(HomeActivity.this, "少爷 , 现在是wifi数据", Toast.LENGTH_LONG).show();
}else if(NetWorkHelper.isNetworkRoaming(getApplicationContext())){
Toast.makeText(getApplicationContext(), "少爷 , 现在是移动数据 !", Toast.LENGTH_LONG).show();
}else if(NetWorkHelper.isNetworkAvailable(getApplicationContext())){
Toast.makeText(getApplicationContext(), "少爷 , 现在有网哦!", Toast.LENGTH_LONG).show();
}else if(NetWorkHelper.isMobileDataEnable(getApplicationContext())){
Toast.makeText(HomeActivity.this, "少爷 , 请检查网络,现在没网哦!", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void Listener() {
// TODO Auto-generated method stub
home_but_home.setOnClickListener(this);
home_but_sort.setOnClickListener(this);
home_but_cart.setOnClickListener(this);
home_but_user.setOnClickListener(this);
}
private void initLocation(){
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy
);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
int span=1000;
option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);//可选,默认false,设置是否使用gps
option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
mLocationClient.setLocOption(option);
mLocationClient.start();
}
private void init() {
// TODO Auto-generated method stub
home_but_home = (Button) findViewById(R.id.home_but_home);
home_but_sort = (Button) findViewById(R.id.home_but_sort);
home_but_cart = (Button) findViewById(R.id.home_but_cart);
home_but_user = (Button) findViewById(R.id.home_but_user);
home_but_home_tv = (TextView)findViewById(R.id.home_but_home_tv);
home_but_sort_tv = (TextView)findViewById(R.id.home_but_sort_tv);
home_but_cart_tv = (TextView)findViewById(R.id.home_but_cart_tv);
home_city_name = (TextView) findViewById(R.id.home_city_name);
home_but_user_tv = (TextView)findViewById(R.id.home_but_user_tv);
home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_selected);
home_but_home_tv.setTextColor(Color.RED);
}
private void fragmanager() {
// TODO Auto-generated method stub
//创建fragment
allfrag = new Fragment[4];
//获取事务管理
manger = getSupportFragmentManager();
allfrag[0] = manger.findFragmentById(R.id.home_fragment_home);
allfrag[1] = manger.findFragmentById(R.id.home_fragment_sort);
allfrag[2] = manger.findFragmentById(R.id.home_fragment_cart);
allfrag[3] = manger.findFragmentById(R.id.home_fragment_user);
//启动事物
trans = manger.beginTransaction()
.hide(allfrag[0])
.hide(allfrag[1])
.hide(allfrag[2])
.hide(allfrag[3]);
trans.show(allfrag[0]).commit();
}
public void onClick(View v) {
// TODO Auto-generated method stub
trans = manger.beginTransaction()
.hide(allfrag[0])
.hide(allfrag[1])
.hide(allfrag[2])
.hide(allfrag[3]);
//获取点击按钮的ID
switch (v.getId()) {
case R.id.home_but_home:
trans.show(allfrag[0]).commit();
home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_selected);
home_but_sort.setBackgroundResource(R.drawable.bottom_tab_classify_normal);
home_but_cart.setBackgroundResource(R.drawable.bottom_tab_shopping_normal);
home_but_user.setBackgroundResource(R.drawable.bottom_tab_user_normal);
home_but_home_tv.setTextColor(Color.RED);
home_but_sort_tv.setTextColor(Color.BLACK);
home_but_cart_tv.setTextColor(Color.BLACK);
home_but_user_tv.setTextColor(Color.BLACK);
break;
case R.id.home_but_sort:
trans.show(allfrag[1]).commit();
home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_normal);
home_but_sort.setBackgroundResource(R.drawable.bottom_tab_classify_selected);
home_but_cart.setBackgroundResource(R.drawable.bottom_tab_shopping_normal);
home_but_user.setBackgroundResource(R.drawable.bottom_tab_user_normal);
home_but_home_tv.setTextColor(Color.BLACK);
home_but_sort_tv.setTextColor(Color.RED);
home_but_cart_tv.setTextColor(Color.BLACK);
home_but_user_tv.setTextColor(Color.BLACK);
break;
case R.id.home_but_cart:
trans.show(allfrag[2]).commit();
home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_normal);
home_but_sort.setBackgroundResource(R.drawable.bottom_tab_classify_normal);
home_but_cart.setBackgroundResource(R.drawable.bottom_tab_shopping_selected);
home_but_user.setBackgroundResource(R.drawable.bottom_tab_user_normal);
home_but_home_tv.setTextColor(Color.BLACK);
home_but_sort_tv.setTextColor(Color.BLACK);
home_but_cart_tv.setTextColor(Color.RED);
home_but_user_tv.setTextColor(Color.BLACK);
break;
case R.id.home_but_user:
trans.show(allfrag[3]).commit();
home_but_home.setBackgroundResource(R.drawable.bottom_tab_home_normal);
home_but_sort.setBackgroundResource(R.drawable.bottom_tab_classify_normal);
home_but_cart.setBackgroundResource(R.drawable.bottom_tab_shopping_normal);
home_but_user.setBackgroundResource(R.drawable.bottom_tab_user_selected);
home_but_home_tv.setTextColor(Color.BLACK);
home_but_sort_tv.setTextColor(Color.BLACK);
home_but_cart_tv.setTextColor(Color.BLACK);
home_but_user_tv.setTextColor(Color.RED);
break;
default:
break;
}
}
public class MyLocationListener implements BDLocationListener {
public void onReceiveLocation(BDLocation location) {
String city = location.getCity();
// String district = location.getDistrict();
// String street = location.getStreet();
// Address address = location.getAddress();
// Log.i("Main", address.toString());
home_city_name.setText(city);// + "---" + street+" "+distric
}
}
}
标签:
原文地址:http://www.cnblogs.com/taogev5/p/5714726.html