android天气,android实现天气预报代码-成都快上网建站

android天气,android实现天气预报代码

android开发 怎么显示天气

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

创新互联是一家从事企业网站建设、成都网站设计、网站制作、行业门户网站建设、网页设计制作的专业网站设计公司,拥有经验丰富的网站建设工程师和网页设计人员,具备各种规模与类型网站建设的实力,在网站建设领域树立了自己独特的设计风格。自公司成立以来曾独立设计制作的站点近1000家。

工具/原料

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

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

手机桌面上怎样显示时间和天气

手机屏幕上显示时间和天气的方法如下:

工具/原料:vivo5x、Android5

1、将手机处于解锁状态,长按手机底部菜单按钮选项,在打开的页面中,点击“添加工具”菜单选项。

2、在打开的小工具栏中,点击选择“天气”小工具,将其添加到桌面上。

3、最后,就可以在手机桌面上看到已经显示了天气和时间了。

android怎么获取实时天气

准备工作:

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

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

一、打开GPS

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

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

四、点“设定”

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

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

七、点“确定”退出

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

九、提示

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

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


本文名称:android天气,android实现天气预报代码
网页路径:http://kswjz.com/article/dsssojo.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流