当前位置:首页 > 网络黑客 > 正文内容

天气预报开发安卓(安卓系统天气预报)

hacker2年前 (2022-12-15)网络黑客116

本文目录一览:

android 做一个天气预报的步骤

安卓编程设计很多方面,非常复杂,需要系统的学习才可以,这里以一个简单的天气预报app编程为例:

public class WebServiceUtil

{

// 定义Web Service的命名空间

static final String SERVICE_NS = "";

// 定义Web Service提供服务的URL

static final String SERVICE_URL = "";

public static List getProvinceList()

{

// 需要调用的 *** 名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)

String methodName = "getRegionProvince";

// 创建HttpTransportSE传输对象

HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);

httpTranstation.debug = true;

// 使用SOAP1.1协议创建Envelop对象

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

SoapEnvelope.VER11);

// 实例化SoapObject对象

SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);

envelope.bodyOut = soapObject;

// 设置与.Net提供的Web Service保持较好的兼容性

envelope.dotNet = true;

try

{

// 调用Web Service

httpTranstation.call(SERVICE_NS + methodName, envelope);

if (envelope.getResponse() != null)

{

// 获取服务器响应返回的SOAP消息

SoapObject result = (SoapObject) envelope.bodyIn;

SoapObject detail = (SoapObject) result.getProperty(methodName

+ "Result");

// 解析服务器响应的SOAP消息。

return parseProvinceOrCity(detail);

}

} catch (Exception e)

{

e.printStackTrace();

}

return null;

}

public static List getCityListByProvince(String province)

{

// 需要调用的 *** 名(获得本天气预报Web Services支持的城市信息,根据省份查询城市 *** :带参数)

String methodName = "getSupportCityString";

HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);

httpTranstation.debug = true;

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

SoapEnvelope.VER11);

SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);

soapObject.addProperty("theRegionCode", province);

envelope.bodyOut = soapObject;

envelope.dotNet = true;

try

{

// 调用Web Service

httpTranstation.call(SERVICE_NS + methodName, envelope);

if (envelope.getResponse() != null)

{

// 获取服务器响应返回的SOAP消息

SoapObject result = (SoapObject) envelope.bodyIn;

SoapObject detail = (SoapObject) result.getProperty(methodName

+ "Result");

// 解析服务器响应的SOAP消息。

return parseProvinceOrCity(detail);

}

} catch (Exception e)

{

e.printStackTrace();

}

return null;

}

private static List parseProvinceOrCity(SoapObject detail)

{

ArrayList result = new ArrayList();

for (int i = 0; i detail.getPropertyCount(); i++)

{

String str = detail.getProperty(i).toString();

// 解析出每个省份

result.add(str.split(",")[0]);

}

return result;

}

public static SoapObject getWeatherByCity(String cityName)

{

// 根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数

String methodName = "getWeather";

HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);

httpTranstation.debug = true;

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

SoapEnvelope.VER11);

SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);

soapObject.addProperty("theCityCode", cityName);

envelope.bodyOut = soapObject;

envelope.dotNet = true;

try

{

// 调用Web Service

httpTranstation.call(SERVICE_NS + methodName, envelope);

if (envelope.getResponse() != null)

{

// 获取服务器响应返回的SOAP消息

SoapObject result = (SoapObject) envelope.bodyIn;

SoapObject detail = (SoapObject) result.getProperty(methodName

+ "Result");

// 解析服务器响应的SOAP消息。

return detail;

}

} catch (Exception e)

{

e.printStackTrace();

}

return null;

}

}

android开发 怎么显示天气

本经验将介绍Android如何获取天气预报主要使用了中国天气网的接口,使用webView显示。

工具/原料

Android Studio

*** /步骤

首先我们打开下载安装好的Android Studio然后新建一个项目,我这里为了方便就直接添加一个Activity了

然后我们添加界面布局代码,布局如下:

?xml version="1.0" encoding="utf-8"?

LinearLayout xmlns:android=""

android:orientation="vertical"

android:gravity="center_horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

LinearLayout

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

Button

android:id="@+id/bj"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/bj" /

Button

android:id="@+id/sh"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/sh" /

Button

android:id="@+id/heb"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/heb" /

Button

android:id="@+id/cc"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/cc" /

Button

android:id="@+id/sy"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/sy" /

Button

android:id="@+id/gz"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/gz" /

/LinearLayout

WebView android:id="@+id/webView1"

android:layout_width="wrap_content"

android:layout_height="0dip"

android:focusable="false"

android:layout_weight="1"

/

/LinearLayout

然后我们添加后台代码:

package com.basillee.asus.demo;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Button;

public class MainActivity7 extends Activity implements OnClickListener {

private WebView webView; //声明WebView组件的对象

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_activity7);

webView=(WebView)findViewById(R.id.webView1); //获取WebView组件

webView.getSettings().setJavaScriptEnabled(true); //设置JavaScript可用

webView.setWebChromeClient(new WebChromeClient()); //处理JavaScript对话框

webView.setWebViewClient(new WebViewClient()); //处理各种通知和请求事件,如果不使用该句代码,将使用内置浏览器访问网页

webView.loadUrl(" "); //设置默认显示的天气预报信息

webView.setInitialScale(57*4); //放网页内容放大4倍

Button bj=(Button)findViewById(R.id.bj); //获取布局管理器中添加的“北京”按钮

bj.setOnClickListener(this);

Button sh=(Button)findViewById(R.id.sh); //获取布局管理器中添加的“上海”按钮

sh.setOnClickListener(this);

Button heb=(Button)findViewById(R.id.heb); //获取布局管理器中添加的“哈尔滨”按钮

heb.setOnClickListener(this);

Button cc=(Button)findViewById(R.id.cc); //获取布局管理器中添加的“长春”按钮

cc.setOnClickListener(this);

Button sy=(Button)findViewById(R.id.sy); //获取布局管理器中添加的“沈阳”按钮

sy.setOnClickListener(this);

Button gz=(Button)findViewById(R.id.gz); //获取布局管理器中添加的“广州”按钮

gz.setOnClickListener(this);

}

@Override

public void onClick(View view){

switch(view.getId()){

case R.id.bj: //单击的是“北京”按钮

openUrl("101010100T");

break;

case R.id.sh: //单击的是“上海”按钮

openUrl("101020100T");

break;

case R.id.heb: //单击的是“哈尔滨”按钮

openUrl("101050101T");

break;

case R.id.cc: //单击的是“长春”按钮

openUrl("101060101T");

break;

case R.id.sy: //单击的是“沈阳”按钮

openUrl("101070101T");

break;

case R.id.gz: //单击的是“广州”按钮

openUrl("101280101T");

break;

}

}

//打开网页的 ***

private void openUrl(String id){

webView.loadUrl(""+id+" "); //获取并显示天气预报信息

}

}

然后我们点击Android Studio上面的运行按钮:

这里要访问 *** 我们要添加权限:

uses-permission android:name="android.permission.INTERNET" /

6

我们然后可以在模拟器上面可以看到获取的天气情况

求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();

}

}

});

}

}

扫描二维码推送至手机访问。

版权声明:本文由黑客技术发布,如需转载请注明出处。

本文链接:https://w-123.com/212651.html

“天气预报开发安卓(安卓系统天气预报)” 的相关文章

Google 发布第 3 个紧急更新 修复 Chrome 中另一个零日漏洞

Google 今天发布了第 3 个紧急更新,修复了存在于 Chrome 浏览器中的另一个零日漏洞。周四,Google 面向 macOS、Windows 和 Linux 发布了 Chrome 100.0.4898.127 更新,会在未来几天内完成部署。 本次更新修复了追踪为 CVE-2022-1364...

以色列政府数个网站遭遇网络攻击:现正从瘫痪中恢复

以色列似乎正在从一场大规模的网络攻击中恢复过来。据Haaretz和Kan的Amichai Stein报道,攻击者在周一晚上攻陷了几个以色列政府网站,其中包括卫生部、内政部、司法部和福利部网站。总理办公室的网站也受到了影响。以色列国家网络管理局在一份声明中称,现在所有的网站都已重新上线。 虽然以色列政...

育碧通报网络安全事件 全公司已采取重置密码的预防措施

在周四的一份网络安全公告中,育碧(Ubisoft)证实该公司在上周遭遇了一起“网络安全事件”。尽管攻击尝试似乎未能造成破坏,但出于安全方面的考虑,育碧还是采取了全公司范围内的密码重置措施,以防发生其它意外。在此期间,育碧暂停了部分服务,但坚称没有玩家数据受到损害。截止发稿时,该公司旗下所有游戏和服...

谷歌搜索 2021 Webspam 报告:过滤垃圾网站数量是 2020 年的六倍

由周四发布的“网络垃圾”(Webspam)报告可知,谷歌搜索在 2021 年过滤的垃圾网站数量、竟是 2020 年的六倍。据悉,作为 Alphabet 旗下子公司,Google 有一套名为 SpamBrian 的人工智能垃圾过滤系统,并且可在超过 99% 情况下实现“不受垃圾所困扰”(spam-fr...

泄露的 Facebook 工程师文件承认违法使用用户数据 或将面临全球收入 4% 的罚款

Facebook正面临一场世界各地隐私法规“海啸”,这将迫使该公司大幅改变处理用户个人数据的方式。根据外媒获得的一份从Facebook泄露的文件,Facebook这场“劫难”的根源在于,他们自己都搞不清楚用户数据的用途和去向。 这份泄露的文件是由Facebook广告和商业产品团队的隐私工程师去...

Clearview AI 被指违反澳大利亚隐私法 已收集至少 30 亿人面部数据

澳大利亚信息专员发现,Clearview AI 在许多方面违反了澳大利亚的隐私法。在此前的双边调查中发现,该公司的面部识别工具未经同意并以不公平的方式收集澳大利亚人的敏感信息。由澳大利亚信息专员办公室(OAIC)和英国信息专员办公室(ICO)进行的调查发现,Clearview AI 的面部识别工具不...

评论列表

竹祭雾夕
2年前 (2022-12-15)

extView; private TextView pressureTextView; private TextView lightTextView; private float currentTemperat

鸽吻眉妩
2年前 (2022-12-16)

else if (currentLight=SensorManager.LIGHT_OVERCAST) lightStr=

可难雾敛
2年前 (2022-12-15)

ght="wrap_content"android:text="@string/heb" /Buttonandroid:id="@+id/cc"android:layout_width=

鸽吻十鸦
2年前 (2022-12-15)

oat.NaN; private float currentLight=Float.NaN; @Override protected void onCreate(Bundle savedInstanceSta

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。