温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Android中使用Internet获取天气预报

发布时间:2021-05-11 16:29:04 来源:亿速云 阅读:173 作者:Leah 栏目:移动开发

这篇文章将为大家详细讲解有关怎么在Android中使用Internet获取天气预报,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:id="@+id/ll1" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" > 
 
 <LinearLayout 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:layout_weight="4" 
 android:gravity="center" 
 android:orientation="horizontal" > 
 <Button 
 android:id="@+id/beijing" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="北京"/> 
 <Button 
 android:id="@+id/shanghai" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="上海"/> 
 <Button 
 android:id="@+id/haerbin" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="哈尔滨"/> 
 <Button 
 android:id="@+id/changchun" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="长春"/> 
 <Button 
 android:id="@+id/shenyang" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="沈阳"/> 
 <Button 
 android:id="@+id/guangzhou" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="广州"/> 
 </LinearLayout> 
 
 <LinearLayout 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:layout_weight="1" 
 android:orientation="horizontal" > 
 
 <WebView android:id="@+id/webview1" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"/> 
 
 </LinearLayout> 
</LinearLayout>

布局效果如图

怎么在Android中使用Internet获取天气预报

要在AndroidManifest.xml中设置强制横屏(android:screenOrientation="landscape"):

<activity 
 android:name=".MainActivity" 
 android:screenOrientation="landscape" 
 android:label="@string/app_name" > 
 <intent-filter> 
 <action android:name="android.intent.action.MAIN" /> 
 <category android:name="android.intent.category.LAUNCHER" /> 
 </intent-filter> 
</activity>

MainActivity:

package com.example.test; 
 
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 MainActivity extends Activity implements OnClickListener{ 
 private WebView webview; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.main); 
 
 webview=(WebView)findViewById(R.id.webview1); 
 //处理各种通知请求和事件,如果不使用该句代码,将使用内置浏览器访问网页 
 webview.setWebViewClient(new WebViewClient()); 
 webview.getSettings().setJavaScriptEnabled(true);//设置兼容JavaScript 
 webview.setWebChromeClient(new WebChromeClient());//处理JavaScript对话框 
 //设置默认显示的天气预报信息 
 webview.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm"); 
 webview.setInitialScale(57*4);//将网页内容放大四倍 
 
 Button bj=(Button)findViewById(R.id.beijing); 
 bj.setOnClickListener(this); 
 Button sh=(Button)findViewById(R.id.shanghai); 
 sh.setOnClickListener(this); 
 Button hrb=(Button)findViewById(R.id.haerbin); 
 hrb.setOnClickListener(this); 
 Button cc=(Button)findViewById(R.id.changchun); 
 cc.setOnClickListener(this); 
 Button sy=(Button)findViewById(R.id.shenyang); 
 sy.setOnClickListener(this); 
 Button gz=(Button)findViewById(R.id.guangzhou); 
 gz.setOnClickListener(this); 
 } 
 
 @Override 
 public void onClick(View v) { 
 switch (v.getId()) { 
 case R.id.beijing: //单击的是"北京"按钮 
 openUrl("101010100T"); 
 break; 
 case R.id.shanghai: //单击的是"上海"按钮 
 openUrl("101020100T"); 
 break; 
 case R.id.haerbin: //单击的是"哈尔滨"按钮 
 openUrl("101050101T"); 
 break; 
 case R.id.changchun: //单击的是"长春"按钮 
 openUrl("101060101T"); 
 break; 
 case R.id.shenyang: //单击的是"沈阳"按钮 
 openUrl("101070101T"); 
 break; 
 case R.id.guangzhou: //单击的是"广州"按钮 
 openUrl("101280101T"); 
 break; 
 
 
 default: 
 break; 
 } 
 } 
 private void openUrl(String id) { 
 //获取并显示天气预报信息 
 webview.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm?id="+id+""); 
 } 
}

别忘记在AndroidManifest.xml中加入访问网络的权限:

<!-- 添加链接网络的权限 -->
uses-permissionandroid:name="android.permission.INTERNET

关于怎么在Android中使用Internet获取天气预报就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI