/** * 主菜单界面 */ public class MainActivity extends CommonActivity implements OnClickListener { private Intent intent;private Button btn_booking;private Button quarrybutton;private Button btn_message;private String version;private String temp;private String tempId;private String tempNum;private String temptype;private String tempName;private String tempPic;private String tempPrice;private String tempJieShao; private String tempDanWei; private String tempt; private String tabname; private String tabid; private String tabadress; private String tabnum; private String tabfuwuyuan; private JSONArray menulist; private JSONArray menulist1;private JSONArray menulistt; private Bitmap bit; private JSONObject jsonObj = null; private JSONObject jsontableObj = null; private Gallery pictureGallery = null; private int[] picture = { R.drawable.gallery1, R.drawable.gallery2, R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5, R.drawable.gallery6, }; private int index = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.pictureGallery = (Gallery) findViewById(R.id.gallery1); ImageAdapter adapter = new ImageAdapter(this); this.pictureGallery.setAdapter(adapter);// 填充适配器 Timer timer = new Timer();// 实例化一个定时器 timer.schedule(task, 2000, 2000);// 定时器设置 btn_booking = (Button) findViewById(R.id.bookingbutton); btn_booking.setOnClickListener(this); quarrybutton = (Button) findViewById(R.id.quarrybutton); quarrybutton.setOnClickListener(this); btn_message = (Button) findViewById(R.id.messagebutton); btn_message.setOnClickListener(this); cursor = database.rawQuery("SELECT banben FROM banbenhao", null); cursor.moveToFirst(); version = cursor.getString(0); if (tablePro()) { try { database.execSQL("Delete from zhuohaoxinxi"); menulistt = jsontableObj.getJSONArray("list"); for (int k = 0; k <= jsontableObj.getJSONArray("list").length(); k++) { tempt = menulistt.getString(k); tempt = tempt.replace(",", "\n"); tempt = tempt.replace("\"", ""); tempt = tempt.replace("[", ""); tempt = tempt.replace("]", ""); StringBuffer tempbt = new StringBuffer(tempt); int i = k+1; tabadress= tempbt.substring(0, tempbt.indexOf("\n")); tempbt = tempbt.delete(0, tempbt.indexOf("\n") + 1); tabname= tempbt.substring(0, tempbt.indexOf("\n")); tempbt = tempbt.delete(0, tempbt.indexOf("\n") + 1); tabnum= tempbt.substring(0, tempbt.indexOf("\n")); tempbt = tempbt.delete(0, tempbt.indexOf("\n") + 1); tempPrice= tempbt.toString(); database.execSQL("insert into [zhuohaoxinxi] values("+i+",'" + tabname + "','" + tabadress + "','" + tabnum + "','" + tempPrice+ "')"); } } catch (JSONException e) { e.printStackTrace(); } // 判断版本是否最新 if (versionPro()) { // 跳转到MainActivity try { if (jsonObj.getString("rtmsg").equals(version)) { Toast.makeText(this, "版本已经为最新版", Toast.LENGTH_LONG).show(); } else { super.progressDialog = ProgressDialog.show(this, "更新菜单", "正在更新菜单....",true); super.progressDialog.show(); //通过线程来循环调用进度条 super.handler.post(this); database.execSQL("Delete from caidan"); database.execSQL("Delete from liangcai"); database.execSQL("Delete from recai"); database.execSQL("Delete from zhushi"); database.execSQL("Delete from tanglei"); database.execSQL("Delete from yinliao"); database.execSQL("Delete from wankuai"); database.execSQL("insert into [caidan] values(1,401,'二锅头',13.0,'酒类','','','二锅头,酒类,口感好,不上头'," + R.drawable.erguotou + ",0);"); System.out.println(jsonObj.getString("rtmsg")); database.execSQL("update banbenhao set banben="+jsonObj.getString("rtmsg")+" where _id=1"); int j = 1; int recaiid = 1;int liangcaiid = 1;int zhushiid = 1; int yinliaoid = 1;int tangid = 1;int wankuaiid = 1; int sui; menulist = jsonObj.getJSONArray("list"); for (int k = 0; k <= jsonObj.getJSONArray("list").length(); k++) { temp = menulist.getString(k); temp = temp.replace(",", "\n"); temp = temp.replace("\"", ""); temp = temp.replace("[", ""); temp = temp.replace("]", ""); StringBuffer tempb = new StringBuffer(temp); int i = 0; tempId = tempb.substring(0, tempb.indexOf("\n")); tempb = tempb.delete(0, tempb.indexOf("\n") + 1); temptype = tempb.substring(0, tempb.indexOf("\n")); tempb = tempb.delete(0, tempb.indexOf("\n") + 1); tempName = tempb.substring(0, tempb.indexOf("\n")); tempb = tempb.delete(0, tempb.indexOf("\n") + 1); tempPic = tempb.substring(0, tempb.indexOf("\n")); tempPic=tempPic.replace("\\",""); System.out.println(tempPic); tempb = tempb.delete(0, tempb.indexOf("\n") + 1); tempPrice = tempb.substring(0, tempb.indexOf("\n")); tempb = tempb.delete(0, tempb.indexOf("\n") + 1); tempJieShao = tempb.substring(0, tempb.indexOf("\n")); tempb = tempb.delete(0, tempb.indexOf("\n") + 1); tempDanWei = tempb.toString(); temptype=temptype.replace("\"", ""); temptype=temptype.replace("\'", ""); switch (Integer.parseInt(temptype)) { case 1201: tempNum = 200 + recaiid+""; Random ran=new Random(); sui=ran.nextInt(2); String sql="insert into [recai] values("+ recaiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',1)"; sql="insert into [recai] values("+ recaiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',0)"; database.execSQL(sql); database.execSQL("insert into [caidan] values(" + j+ ",'" + tempNum + "','" + tempName + "','"+ tempPrice + "','" + temptype + "','"+ tempDanWei + "',' ','" + tempJieShao+ "','"+tempPic+"',0)"); j++; recaiid++; break; case 1202: tempNum = 100 + liangcaiid+""; Random ran1=new Random(); sui=ran1.nextInt(2); database.execSQL("insert into [liangcai] values("+ liangcaiid + ",'" + tempNum + "','" + tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',1)"); database.execSQL("insert into [liangcai] values("+ liangcaiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',0)"); database.execSQL("insert into [caidan] values(" + j+ ",'" + tempNum + "','" + tempName + "','" + tempPrice + "','" + temptype + "','"+ tempDanWei + "',' ','" + tempJieShao+ "','"+tempPic+"',0)"); j++;liangcaiid++;break; case 1203: tempNum = 500 + tangid+"";Random ran2=new Random(); sui=ran2.nextInt(2); database.execSQL("insert into [tanglei] values("+ liangcaiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',1)"); database.execSQL("insert into [tanglei] values("+ tangid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',0)"); database.execSQL("insert into [caidan] values(" + j + ",'" + tempNum + "','" + tempName + "','"+ tempPrice + "','" + temptype + "','"+ tempDanWei + "',' ','" + tempJieShao + "','"+tempPic+"',0)"); j++; tangid++; break; case 1204: tempNum = 600 + yinliaoid+""; Random ran3=new Random(); sui=ran3.nextInt(2); database.execSQL("insert into [yinliao] values("+ liangcaiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','" + temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',1)") database.execSQL("insert into [yinliao] values("+ yinliaoid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',0)"); database.execSQL("insert into [caidan] values(" + j+ ",'" + tempNum + "','" + tempName + "','" + tempPrice + "','" + temptype + "','"+ tempDanWei + "',' ','" + tempJieShao + "','"+tempPic+"',0)"); j++; yinliaoid++; break; case 1205: tempNum = 300 + zhushiid+""; Random ran4=new Random(); sui=ran4.nextInt(2); database.execSQL("insert into [zhushi] values("+ liangcaiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',1)"); database.execSQL("insert into [zhushi] values("+ zhushiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',0)"); database.execSQL("insert into [caidan] values(" + j+ ",'" + tempNum + "','" + tempName + "','"+ tempPrice + "','" + temptype + "','"+ tempDanWei + "',' ','" + tempJieShao+ "','"+tempPic+"',0)"); j++; zhushiid++; break; case 1206: Random ran5=new Random(); sui=ran5.nextInt(2); database.execSQL("insert into [wankuai] values("+ wankuaiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',1)"); database.execSQL("insert into [wankuai] values("+ wankuaiid + ",'" + tempNum + "','"+ tempName + "','" + tempPrice + "','"+ temptype + "','" + tempDanWei + "',' ','"+ tempJieShao + "','"+tempPic+"',0)"); database.execSQL("insert into [caidan] values(" + j+ ",'" + tempNum + "','" + tempName + "','"+ tempPrice + "','" + temptype + "','"+ tempDanWei + "',' ','" + tempJieShao+ "','"+tempPic+"',0)"); j++; wankuaiid++; break; } } } } catch (JSONException e) { e.printStackTrace(); } } else { Toast.makeText(this, "服务器异常,等待更新……", Toast.LENGTH_LONG).show(); } }} private TimerTask task = new TimerTask() { @Override public void run() { Message message = new Message(); message.what = 2; index = pictureGallery.getSelectedItemPosition(); index++; handler.sendMessage(message); } }; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 2: pictureGallery.setSelection(index); break; } } };class ImageAdapter extends BaseAdapter { private int GalleryItemBackground; private Context context; public ImageAdapter(Context context) { this.context = context; TypedArray typedArray = context .obtainStyledAttributes(R.styleable.Gallery); GalleryItemBackground = typedArray.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0); public int getCount() { return Integer.MAX_VALUE; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); imageView.setImageResource(picture[position % picture.length]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams( Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT)); imageView.setBackgroundResource(GalleryItemBackground); return imageView; }} public void onClick(View v) { switch (v.getId()) { case R.id.bookingbutton: intent = new Intent(this, BookingSeatsActivity.class); break; case R.id.quarrybutton: intent = new Intent(this, MenufindActivity.class); break; case R.id.messagebutton: intent = new Intent(this, ChooseMessageActivity.class); break; } // 不为null,则跳转页面 if (intent != null) { startActivity(intent); // 清空Intent对象 intent = null; this.finish(); } } /** * 重写键盘按键按下是触发该方法 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { new AlertDialog.Builder(MainActivity.this) .setIcon(R.drawable.icon) .setTitle("提示") .setMessage("确认退出?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialoginterface, int i) { System.exit(1); } }) .setNeutralButton("返回", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialoginterface, int i) { } }).show(); } return false; } private boolean versionPro() { try { jsonObj = query(); jsontableObj=zhuoziquery(); if (jsonObj.getString("rt").equals("200")) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; } private boolean tablePro() { try { jsontableObj=zhuoziquery(); if (jsontableObj.getString("rt").equals("200")) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; } /** * 定义发送请求的方法 */ private JSONObject query() throws Exception { // 定义发送请求的URL String url = HttpUtil.UPDATE_URL + "?num=1" + "&update=menu"; JSONObject jsonobject1 = new JSONObject(HttpUtil.getRequest(url)); return jsonobject1; } private JSONObject zhuoziquery() throws Exception { // 定义发送请求的URL String url = HttpUtil.UPDATE_URL + "?num=1" + "&update=table"; JSONObject jsonobject1 = new JSONObject(HttpUtil.getRequest(url)); return jsonobject1; }}