標籤:android style blog http color io os ar 使用
很多時候我們外出,但是由於沒有掌握出行的天氣情況,經常遇到苦惱的事情,成為落湯雞,今天我就帶大家寫
一款關於天氣預報的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>
歡迎介面展示:
在進入活動和服務之前,會對使用者的當前網路狀態進行判斷,如果使用者當前網路狀態不好,或者沒網路,會彈出網路設定介面,供使用者選擇,如果網路沒為題,在播放完此動畫之後,會啟動一個意圖開啟服務,跳轉至主活動
下一篇將講解服務和主活動的使用,這篇內容就這些,知識不是很多,好好消化下,咱們下篇接著說
Android 編程之天氣預報小執行個體源碼示範及效果展示--1