文章目录:
求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();
}
}
});
}
}
求一个能用的android天气预报源代码
我这边有 2227421573 不过是简单的实现天气预报,只能查看当天的天气信息, 自动定位到当前的城市,然后去查询天气信息的.
急!求安卓天气预报源代码,可以直接在Eclipse上运行,可以简单一点,能输入城市查询天气就行
天气查询情况可以用微信城市服务查询。城市服务里面可以查询到的,微信城市服务时最近开始广泛的一个平台。有机动车查询系统。可以在这边查询,除了这个,平时家庭的水电费等都可以在这里查询到账单记录,只要输入账单上的护号就可以
loat currentTemperature=Float.NaN; private float currentPressure=Float.NaN; private float currentLight=Float.NaN;
Listener() { @Override public void onSensorChanged(SensorEvent event) {
ride public void run() { updateGUI(); } },0,1000);
currentTemperature=Float.NaN; private float currentPressure=Float.NaN; private float currentLight=Float.NaN; @Override protected void