Android 编程之天气预报小实例源码演示及效果展示--1

来源:互联网 发布:mac怎么移动窗口 编辑:程序博客网 时间:2024/05/12 21:17

很多时候我们外出,但是由于没有掌握出行的天气情况,经常遇到苦恼的事情,成为落汤鸡,今天我就带大家写

一款关于天气预报的Android APP,我会很详细的把它分成几个部分来详细讲解,希望大家喜欢的可以顶一个,

也同时呢,希望更多的人加入到我的博客中来,一起学习,一起交流,一起进步,谢谢大家!


写完天气预报之后有空闲时间的话会讲文件管理器和其他实用的一些APP开发,方便大家学习,交流


在开发天气预报之前,首先要准备一个获取天气信息的API key ,在这里,笔者向大家推荐一个网址,比较实用

key 申请地址:http://www.wunderground.com

得到 key 之后我们就可以开始我们的开发了,为了使我们的界面看起来更加精简和美观,我用到了 android.support.v4.jar 第三方库 picasso-2.2.0.jar,没有的童鞋可以到 http://download.csdn.net/detail/jspping/8084689 自行下载


下载完之后将他们导入到项目libs文件夹,没有的可以自己新建一个libs,创建好新的 Android Project之后,接下来就开始我们的编程吧,首先我们要在清单文件XML里面给我们的 application 添加对应的权限,我们这里需要用到的是 网络访问,网络状态,GPS权限等,配置清单如下:

    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />    <!-- GPS 、网络 -->     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

权限配置好了,就开始编程了,笔者在这里给 application 添加了一个欢迎的 Animation,为的是给用户更友好的体验,下面看看代码:

package com.newer.myweather;/** * Animation 欢迎动画 * @author Engineer-Jsp * @date 2014.10.27 * */import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.ComponentName;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager.NameNotFoundException;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.Bundle;import android.os.Handler;import android.view.animation.AlphaAnimation;import android.widget.LinearLayout;import android.widget.TextView;public class SplashActivity extends Activity {private TextView versionNumber;private LinearLayout mLinearLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.splash);mLinearLayout = (LinearLayout) findViewById(R.id.LinearLayout01);versionNumber = (TextView) findViewById(R.id.versionNumber);versionNumber.setText(getVersion());if (isNetWorkConnected()) {AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);aa.setDuration(2000);mLinearLayout.setAnimation(aa);mLinearLayout.startAnimation(aa);// 动画线程,与跳转延迟设置,我这里是设置了3000ms,即3秒钟new Handler().postDelayed(new Runnable() {@Overridepublic void run() {// 启动天气服务Intent service = new Intent(SplashActivity.this,WeatherService.class);startService(service);finish();// 欢迎动画完毕跳转至主活动Intent intent = new Intent(SplashActivity.this,MainActivity.class);startActivity(intent);}}, 3000);} else {showSetNetworkDialog();}}    // 弹出网络状态窗口private void showSetNetworkDialog() {AlertDialog.Builder builder = new Builder(this);builder.setTitle("设置网络");builder.setMessage("网络错误请检查网络状态");builder.setPositiveButton("设置网络", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {Intent intent = null;// 判断手机系统的版本 即API大于10 就是3.0或以上版本if (android.os.Build.VERSION.SDK_INT > 10) {intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);} else {intent = new Intent();ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");intent.setComponent(component);intent.setAction("android.intent.action.VIEW");}startActivity(intent);finish();}});builder.setNegativeButton("取消", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {finish();}});builder.create().show();}    // 网络状态情况private boolean isNetWorkConnected() {ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);NetworkInfo networkInfo = manager.getActiveNetworkInfo();return (networkInfo != null && networkInfo.isConnected());}    // 取版本号private String getVersion() {try {PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);return "Version " + info.versionName;} catch (NameNotFoundException e) {e.printStackTrace();return "Version";}}}

加载的splash.xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/LinearLayout01"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/home_gradiend"    android:gravity="center"    android:orientation="vertical" >    <ImageView        android:id="@+id/logo"        android:layout_width="150dp"        android:layout_height="150dp"        android:layout_marginTop="60dip"        android:paddingLeft="20dip"        android:paddingRight="20dip"        android:scaleType="centerInside"        android:src="@drawable/logo" >    </ImageView>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dip"        android:gravity="bottom"        android:shadowColor="#FFFFFF"        android:shadowDx="0"        android:shadowDy="2"        android:shadowRadius="1"        android:text="@string/app_name"        android:textColor="#444444"        android:textSize="35dip"        android:typeface="serif" >    </TextView>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dip"        android:gravity="bottom"        android:shadowColor="#FFFFFF"        android:shadowDx="0"        android:shadowDy="2"        android:shadowRadius="1"        android:text="(天气预报客户端)"        android:textColor="#444444"        android:textSize="25dip"        android:typeface="serif" >    </TextView>    <TextView        android:id="@+id/versionNumber"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dip"        android:gravity="bottom"        android:shadowColor="#FFFFFF"        android:shadowDx="0"        android:shadowDy="2"        android:shadowRadius="1"        android:text="版本号"        android:textColor="#444444"        android:textSize="20dip"        android:typeface="serif" >    </TextView></LinearLayout>

欢迎界面效果图展示:


在进入活动和服务之前,会对用户的当前网络状态进行判断,如果用户当前网络状态不好,或者没网络,会弹出网络设置界面,供用户选择,如果网络没为题,在播放完此动画之后,会启动一个意图开启服务,跳转至主活动

下一篇将讲解服务和主活动的使用,这篇内容就这些,知识不是很多,好好消化下,咱们下篇接着说

1 0