Android天气Api接口_百度天气预报源码下载
程序员文章站
2022-03-01 12:44:56
...
本Demo是一个基于安卓的天气预报项目源码、主界面使用了DrawerLayout + Fragment控件、集成百度定位SDK、天气数据来自百度天气API、这类联网的项目说不定什么时候api一变动就不能用了、所以多截了几张图防止到时候看不到什么效果、到时候大家可以通过百度的api调整、不过一般情况下是不会发生变化的
onCreate代码
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.launch_activity); pDialog = new ProgressDialog(this); pDialog.setCancelable(false); pDialog.setMessage("定位中..."); pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); pDialog.show(); mLocationClient = new LocationClient(this.getApplicationContext()); mListener = new MyLocationListener(); mLocationClient.registerLocationListener(mListener);// 注册监听函数 LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy); option.setIsNeedAddress(true); mLocationClient.setLocOption(option); mLocationClient.start(); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(2000);// 注:异步线程中不能设置UI } catch (InterruptedException e) { e.printStackTrace(); } sendRequest(); } }).start(); }
对网络连接状态做处理代码
Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if(pDialog != null){ pDialog.dismiss(); } if (msg != null) switch (msg.arg1) { case succeed:// 与服务器连接成功,则传递数据并跳转 Intent intent = new Intent(LaunchActivity.this, HomePagerActivity.class); if (msg.obj != null) intent.putExtra("weather_data", (String) msg.obj); intent.putExtra("normal_city", normalCity); startActivity(intent); finish(); break; case fail:// 与服务器连接失败,弹出错误提示Toast Toast.makeText(LaunchActivity.this, getString(R.string.net_fail), Toast.LENGTH_SHORT) .show(); Message Mesg = Message.obtain(); Mesg.arg1 = nonet;// Handler机制,同抽奖类APP handler.sendMessageDelayed(Mesg, 2000);// 延迟发送 break; case nonet: finish();// 2秒后关闭页面 break; } } };
源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1jHk6hPO 密码: 774v
上一篇: 新年要吃什么美食