Android中获取天气,android中获取即刻天气

android中获取即时天气

先看效果:

162538307.png

162538308.png

162538309.png

需求非常简单,在菜单中加入天气查询的按钮,点击后显示即时天气。

准备工作:

1.下载华为能力SDK;http://imax.vmall.com/nj-campus/universityEpDown/toDownPage

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

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

具体步骤如下:

1.创建工程

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

162538310.png

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;i

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

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

+mLocation.getLongitude()+"\ntmperature:"+tmp[13]) .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文件中添加权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值