求Android天气预报的开发源代码
package com.nrzc.weatherstation;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
/**
* 环境传感器
* 气象站
*/
public class MainActivity extends AppCompatActivity {
private SensorManager sensorManager;
private TextView temperatureTextView;
private TextView pressureTextView;
private TextView lightTextView;
private float currentTemperature=Float.NaN;
private float currentPressure=Float.NaN;
private float currentLight=Float.NaN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Timer updateTimer=new Timer("weatherUpdate");
updateTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateGUI();
}
},0,1000);
}
private void init(){
temperatureTextView=(TextView)findViewById(R.id.temperature);
pressureTextView=(TextView)findViewById(R.id.pressure);
lightTextView=(TextView)findViewById(R.id.light);
sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
private final SensorEventListener tempSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentTemperature=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
private final SensorEventListener pressureSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentPressure=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
private final SensorEventListener lightSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentLight=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
@Override
protected void onResume() {
super.onResume();
Sensor lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (lightSensor!=null)
sensorManager.registerListener(lightSensorEventListener,
lightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
else
lightTextView.setText("Light Sensor Unavailable");
Sensor pressureSensor=sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
if (pressureSensor!=null)
sensorManager.registerListener(pressureSensorEventListener,
pressureSensor,SensorManager.SENSOR_DELAY_NORMAL);
else
pressureTextView.setText("Barometer Unavailable");
Sensor temperatureSensor=sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (temperatureSensor!=null)
sensorManager.registerListener(tempSensorEventListener,
temperatureSensor,
SensorManager.SENSOR_DELAY_NORMAL);
else
temperatureTextView.setText("Thermometer Unavailable");
}
@Override
protected void onPause() {
sensorManager.unregisterListener(pressureSensorEventListener);
sensorManager.unregisterListener(tempSensorEventListener);
sensorManager.unregisterListener(lightSensorEventListener);
super.onPause();
}
private void updateGUI(){
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!Float.isNaN(currentPressure)){
pressureTextView.setText(currentPressure+"hPa");
pressureTextView.invalidate();
}
if (!Float.isNaN(currentLight)){
String lightStr="Sunny";
if (currentLight=SensorManager.LIGHT_CLOUDY)
lightStr="night";
else if (currentLight=SensorManager.LIGHT_OVERCAST)
lightStr="Cloudy";
else if (currentLight=SensorManager.LIGHT_SUNLIGHT)
lightStr="Overcast";
lightTextView.setText(lightStr);
lightTextView.invalidate();
}
if (!Float.isNaN(currentTemperature)){
temperatureTextView.setText(currentTemperature+"C");
temperatureTextView.invalidate();
}
}
});
}
}
安卓天气预报源码,从网络获取获取信息那种的,求大神教教我
你要求太多了,除非是干这行的且有相关项目。即使如此也不会轻易给你。既然你是答辩,那么可以给你大概说下都是什么东西。关键就是一个后台service,设置成24小时一次访问网络,获取数据,至于天气预报信息,是从服务器请求下来的数据。其他的就是把这些数据适配到界面的问题了。‘ 建议你去百度上搜下: 1,Activity组件 2,service组件 3,webService 4,Adapter 当让,以上都是安卓相关。 既然是毕业设计,就花点时间看下吧。记得当时我也选个自己不怎么会的项目,然后钻了一段时间,最后毕业答辩虽然说的不好,可是都是自己的东西,老师们也知道大学情况,看到有人能自己努力不去抄袭,也很感动的给了好评。我谢谢那些令人尊敬的老师给我的鼓励。加油吧... ...
如何自己动手做一个android天气预报的app
一个Android天气预报要用到的代码文件,可显示操作菜单,显示未来四天天气、将全国城市列表写入数据库、初始化数据解析类,界面控件,消息接收器Handler、线程消息接收器、左侧菜单选择事件监听、后退按钮监听等,代码为:
227
if(mycitys.size()==1){
228
showToast("至少要保留一个城市");
229
}else{
230
mycitys.remove(position);
231
writeMyCitys();
232
sAdapter.notifyDataSetChanged();
233
currCityId=mycitys.get(0).get("cityId");
234
changeSearchState();
235
}
236
}
237
});
238
builder.setNegativeButton("取 消",
239
new DialogInterface.OnClickListener() {
240
public void onClick(DialogInterface dialog, int which) {
241
dialog.dismiss();
242
}
243
});
244
builder.show();
245
return false;
246
}
247
//
监听后退按钮
248
@Override
249
public boolean
onKeyDown(int
keyCode, KeyEvent event) {
250
if (keyCode ==
KeyEvent.KEYCODE_BACK) {
251
// 如果当前menu没有显示
252
if (!isBack) {
253
showToast("再按一次退出");
254
downTime = event.getDownTime();
255
isBack = true;
256
return true;
257
} else {
258
if (event.getDownTime() -
downTime = 2000) {
259
AppManager.getAppManager().AppExit(Weather.this);
260
} else {
261
showToast("再按一次退出");
262
downTime = event.getDownTime();
263
return true;
264
}
265
}
266
}
267
return super.onKeyDown(keyCode, event);
268
}
269
@Override
270
protected void
onDestroy() {
271
if(mVibrator!=null){
272
mVibrator.cancel();
273
}
274
super.onDestroy();
275
}
276
}
【Weather Forecast】用Eclipse给Android写一个简单的天气预报app,求助功能部分,xml布局没问题
如果你要天气预报JSON解析的接口,我可以给你,但是要做出你上边这些功能。那还是算了吧。
还木有评论哦,快来抢沙发吧~