android天气,android天气app开发

android怎么获取实时天气

准备工作:

十年的额敏网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整额敏建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“额敏网站设计”,“额敏网站推广”以来,每个客户项目都认真落实执行。

1.下载华为能力SDK;

2.申请一个应用获取appId和appkey,待会要用到。

简单的思路就是先通过网络或者gps获取到当前位置的经纬度,然后通过sdk查询温度,获取结果。

具体步骤如下:

1.创建工程

把sdk中jar包拖到工程中的libs文件夹下面。

2.主类代码如下

package com.empty.weatherreport;

import com.empty.weatherreport.WeatherUtil.SCell;

import com.empty.weatherreport.WeatherUtil.SItude;

import com.imax.vmall.sdk.android.common.adapter.ServiceCallback;

import com.imax.vmall.sdk.android.entry.CapabilityService;

import com.imax.vmall.sdk.android.entry.CommonService;

import com.imax.vmall.sdk.android.huawei.weather.WeatherService;

import android.location.Location;

import android.location.LocationManager;

import android.os.Bundle;

import android.os.Handler;

import android.os.Looper;

import android.os.Message;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.content.Context;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.TextView;

import android.widget.Toast;

public class WeatherActivity extends Activity {

private MyHandler myHandler;

private ProgressDialog mProgressDialog;

private Location mLocation;

private boolean sdkStatus;

//Tool to get weather

/**

* CommonService

*/

private CommonService cs;

/**

* WeatherService

*/

private WeatherService weather;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_weather);

sdkStatus=false;

myHandler=new MyHandler();

//初始化业务接口实例

weather = CapabilityService.getWeatherServiceInstance();

//实例化CommonService

cs=CommonService.getInstance();

initSDK();

}

private void initSDK()

{

//应用ID,请去iMAX平台注册申请

String appId="******";

//应用Key

String appKey="******";

//通过CommonService调用鉴权接口,在调用其它能力前必须保证鉴权初始化成功

cs.init(WeatherActivity.this,appId, appKey, new ServiceCallback() {

public void onError(String arg0) {

// TODO Auto-generated method stub

//设置消息

Message msg = new Message();

msg = new Message();

msg.what = 2;

msg.obj = "SDK initialize failed!";

myHandler.sendMessage(msg);

}

public void onComplete(String arg0) {

// TODO Auto-generated method stub

//设置消息

Message msg = new Message();

msg = new Message();

msg.what = 2;

msg.obj = "SDK initialize success!";

sdkStatus=true;

myHandler.sendMessage(msg);

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.activity_weather, menu);

return true;

}

@Override

public boolean onMenuItemSelected(int featureId, MenuItem item) {

// TODO Auto-generated method stub

if(item.getItemId()==R.id.menu_settings) Toast.makeText(getApplicationContext(), "Ha", Toast.LENGTH_SHORT).show();

if(item.getItemId()==R.id.menu_weather)

{

if(sdkStatus)

{

/** 弹出一个等待状态的框 */

mProgressDialog = new ProgressDialog(this);

mProgressDialog.setMessage("Waiting...");

mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

mProgressDialog.show();

WeatherThread m = new WeatherThread();

new Thread(m).start();

}

else

Toast.makeText(getApplicationContext(), "SDK not installed", Toast.LENGTH_SHORT).show();

}

return super.onMenuItemSelected(featureId, item);

}

/** 显示结果 */

private void showResult(String s) {

String tmp[]=s.split("\"");

for(int i=0;itmp.length;i++)

Log.i("tmp"+i, tmp[i]);

new AlertDialog.Builder(this) .setTitle("Weather") .setMessage("latitude:"+mLocation.getLatitude()+"\n longitude:"

+mLocation.getLongitude()+"\ntmperature:"+tmp[21]) .show();

}

class MyHandler extends Handler {

public MyHandler() {

}

public MyHandler(Looper L) {

super(L);

}

// 子类必须重写此方法,接管数据

@Override

public void handleMessage(Message msg) {

// TODO Auto-generated method stub

Log.d("MyHandler", "handleMessage......");

/** 显示结果 */

switch(msg.what)

{

case 1:

Log.i("Error", "case1");

mProgressDialog.dismiss();

showResult((String)msg.obj);

break;

case 2:

Toast.makeText(getApplicationContext(), (String)msg.obj, Toast.LENGTH_SHORT).show();

break;

default:;

}

super.handleMessage(msg);

// 此处可以更新UI

}

}

class WeatherThread implements Runnable {

public void run() {

final Message msg = new Message();

msg.what=1;

try {

mLocation=getLocation(WeatherActivity.this);

weather.getWeather(Double.toString(mLocation.getLongitude()),Double.toString(mLocation.getLatitude()), new ServiceCallback()

{

public void onError(String arg0)

{

//api接口调用错误响应

Log.i("Error", "getWeather error:"+arg0);

//设置消息

msg.obj = arg0;

/** 关闭对话框 */

myHandler.sendMessage(msg); // 向Handler发送消息,更新UI

}

public void onComplete(String arg0)

{

//api接口调用成功响应

Log.i("Complete", "getWeather complete:"+arg0);

//设置消息

msg.obj = arg0;

/** 关闭对话框 */

myHandler.sendMessage(msg); // 向Handler发送消息,更新UI

}

});

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

//Get the Location by GPS or WIFI

public Location getLocation(Context context) {

LocationManager locMan = (LocationManager) context

.getSystemService(Context.LOCATION_SERVICE);

Location location = locMan

.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location == null) {

location = locMan

.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

}

return location;

}

}

3.载manifest文件中添加权限

uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"

/uses-permission

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

/uses-permission

uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"

/uses-permission

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

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

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 如何实现获取天气预报信息?

方法步骤(以安卓5.0为例)

一、打开GPS

二、点主屏的“天气”图标

三、点右上角三个点的图标

四、点“设定”

五、在下图右侧的三个红圈处打上钩,然后点“自动刷新”。

六、根据自己的需要选择自动刷新的时间

七、点“确定”退出

八、可以随时点刷新图标来手动刷新天气。

九、提示

1.适时刷新天气需要开启GPS定位。

2.在没有WIFI的地方刷新天气需要消耗一定的流量,刷新频率越高,消耗流量越多。

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 天气 App

 首推彩虹天气

国人开发,与墨迹天气使用同一个天气源,该有的功能基本都有,但是整个软件只有不到500k,简单小巧,预报较为准确,无广告。缺点就是插件皮肤略显屌丝,可供选择的皮肤较少,而且桌面插件不支持快捷操作。

墨迹天气

应该是目前国内天气类应用安装量头把交椅。皮肤绚丽,还举办过两届墨迹皮肤设计大赛,涌现出许多不错的皮肤创意。功能强大,除基本功能外,还支持天气语音播报,天气时景相机,动态天气皮肤,温度/风力趋势表,空气指数,黄历查询等。国人的很多软件有个特点,经历过一段时间的发展后,总喜欢做「大而全」,期待墨迹以后的版本加入话费查询、视频聊天、病毒查杀等功能。一个小缺点就是广告略多,免费软件加入广告无可厚非,但是一个天气广告竟然有八个广告插件,未免夸张。

天气通

新浪旗下软件,国内较早的天气类应用,支持多平台。功能丰富,支持天气趋势,空气质量指数等,在中国好声音火的那段时间还适时推出了好声音选手播报天气功能。整体比较不错,但是之前应用出现了一些细节问题,用户跑到论坛区提意见,估计去的人有点多,结果论坛的安卓板块就给......关闭了-_-!

WeatherBug

下载量曾经长期在Google

Play天气类应用中保持第一名,Elite版本市价12.38元人民币。功能只能用强大来形容,号称业界最精准的Pin-Point视觉预测,同时集成了交互式地图以及卫星云图,还独家DTAs天气预警系统,能提供比其他预警速度快50%的恶劣天气预警服务,更多功能只等你去发现。缺点,无中文。

Eye In Sky Weather

原来的weather eye

pro,干净清爽,UI大赞,支持12套图标,4种桌面插件,还可以自定义。如果有人提问「请推荐一款优雅的天气应用」的话,非此货莫属。目前用的就是它。缺点:但是貌似定位需要手动输入,而且桌面插件不支持显示时间。


当前标题:android天气,android天气app开发
链接URL:http://ybzwz.com/article/dsdoeej.html