編輯:關於Android編程
分析:
此功能必須在可用的網絡下運行的,確保在有可用網絡下才能正常運行,獲取當前網絡IP判斷所在城市,通過城市查詢天氣
1、首先判斷網絡是否正常(筆者做的是平板應用的一個模塊有手機有些功能不一樣)
[java]
public void getLocalIPAddress() {
WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wm.isWifiEnabled()) {
Toast.makeText(this, "沒有可用的網絡", Toast.LENGTH_LONG).show();
}
}
2、其次要自動獲取IP地址(webservice借口、相關網址),在此,筆者是根據網址獲取,在進行解析
getCityIP()
[java]
public void getCityIP() {
URL url;
URLConnection conn = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
String str = "";
org.jsoup.nodes.Document doc;
try {
url = new URL("asp">http://city.ip138.com/city.asp");
conn = url.openConnection();
is = conn.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String input = "";
while ((input = br.readLine()) != null) {
str += input;
}
doc = Jsoup.parse(str);
String ip1 = doc.body().text();
int start = ip1.indexOf("[");
int end = ip1.indexOf("]");
setIp(ip1.substring(start + 1, end));
} catch (Exception e) {
e.printStackTrace();
}
}
3、再次根據IP地址獲取城市(webservice借口、解析網頁)
getCityByIp():
[java]
public void getCityByIp() {
try {
URL url = new URL("jsp?ip">http://whois.pconline.com.cn/ip.jsp?ip=" + getIp());
HttpURLConnection connect = (HttpURLConnection) url
.openConnection();
InputStream is = connect.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buff = new byte[256];
int rc = 0;
while ((rc = is.read(buff, 0, 256)) > 0) {
outStream.write(buff, 0, rc);
}
System.out.println(outStream);
byte[] b = outStream.toByteArray();
// 關閉
outStream.close();
is.close();
connect.disconnect();
String address = new String(b,"GBK");
if (address.startsWith("北")||address.startsWith("上")||address.startsWith("重")){
setCity(address.substring(0,address.indexOf("市")));
}
if(address.startsWith("香")){
setCity(address.substring(0,address.indexOf("港")));
}
if(address.startsWith("澳")){
setCity(address.substring(0,address.indexOf("門")));
}
if (address.indexOf("省") != -1) {
setCity(address.substring(address.indexOf("省") + 1, address.indexOf("市")));
}
} catch (Exception e) {
e.printStackTrace();
}
}
4、進行天氣查詢實現webservice接口(http://www.webxml.com.cn/webservices/weatherwebservice.asmx)
[java]
private static final String NAMESPACE = "http://WebXml.com.cn/";
// WebService地址
private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
private static final String METHOD_NAME = "getWeatherbyCityName";
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
private String weatherToday;
private SoapObject detail;
private String weatherNow;
private String weatherWillBe;
private void setIcon(String weather, ImageView imageview) {
if (weather.equalsIgnoreCase("nothing.gif"))
imageview.setBackgroundResource(R.drawable.a_nothing);
if (weather.equalsIgnoreCase("0.gif"))
imageview.setBackgroundResource(R.drawable.a_0);
if (weather.equalsIgnoreCase("1.gif"))
imageview.setBackgroundResource(R.drawable.a_1);
if (weather.equalsIgnoreCase("2.gif"))
imageview.setBackgroundResource(R.drawable.a_2);
if (weather.equalsIgnoreCase("3.gif"))
imageview.setBackgroundResource(R.drawable.a_3);
if (weather.equalsIgnoreCase("4.gif"))
imageview.setBackgroundResource(R.drawable.a_4);
if (weather.equalsIgnoreCase("5.gif"))
imageview.setBackgroundResource(R.drawable.a_5);
if (weather.equalsIgnoreCase("6.gif"))
imageview.setBackgroundResource(R.drawable.a_6);
if (weather.equalsIgnoreCase("7.gif"))
imageview.setBackgroundResource(R.drawable.a_7);
if (weather.equalsIgnoreCase("8.gif"))
imageview.setBackgroundResource(R.drawable.a_8);
if (weather.equalsIgnoreCase("9.gif"))
imageview.setBackgroundResource(R.drawable.a_9);
if (weather.equalsIgnoreCase("10.gif"))
imageview.setBackgroundResource(R.drawable.a_10);
if (weather.equalsIgnoreCase("11.gif"))
imageview.setBackgroundResource(R.drawable.a_11);
if (weather.equalsIgnoreCase("12.gif"))
imageview.setBackgroundResource(R.drawable.a_12);
if (weather.equalsIgnoreCase("13.gif"))
imageview.setBackgroundResource(R.drawable.a_13);
if (weather.equalsIgnoreCase("14.gif"))
imageview.setBackgroundResource(R.drawable.a_14);
if (weather.equalsIgnoreCase("15.gif"))
imageview.setBackgroundResource(R.drawable.a_15);
if (weather.equalsIgnoreCase("16.gif"))
imageview.setBackgroundResource(R.drawable.a_16);
if (weather.equalsIgnoreCase("17.gif"))
imageview.setBackgroundResource(R.drawable.a_17);
if (weather.equalsIgnoreCase("18.gif"))
imageview.setBackgroundResource(R.drawable.a_18);
if (weather.equalsIgnoreCase("19.gif"))
imageview.setBackgroundResource(R.drawable.a_19);
if (weather.equalsIgnoreCase("20.gif"))
imageview.setBackgroundResource(R.drawable.a_20);
if (weather.equalsIgnoreCase("21.gif"))
imageview.setBackgroundResource(R.drawable.a_21);
if (weather.equalsIgnoreCase("22.gif"))
imageview.setBackgroundResource(R.drawable.a_22);
if (weather.equalsIgnoreCase("23.gif"))
imageview.setBackgroundResource(R.drawable.a_23);
if (weather.equalsIgnoreCase("24.gif"))
imageview.setBackgroundResource(R.drawable.a_24);
if (weather.equalsIgnoreCase("25.gif"))
imageview.setBackgroundResource(R.drawable.a_25);
if (weather.equalsIgnoreCase("26.gif"))
imageview.setBackgroundResource(R.drawable.a_26);
if (weather.equalsIgnoreCase("27.gif"))
imageview.setBackgroundResource(R.drawable.a_27);
if (weather.equalsIgnoreCase("28.gif"))
imageview.setBackgroundResource(R.drawable.a_28);
if (weather.equalsIgnoreCase("29.gif"))
imageview.setBackgroundResource(R.drawable.a_29);
if (weather.equalsIgnoreCase("30.gif"))
imageview.setBackgroundResource(R.drawable.a_30);
if (weather.equalsIgnoreCase("31.gif"))
imageview.setBackgroundResource(R.drawable.a_31);
}
public void getWeather(String cityName) {
try {
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty("theCityName", cityName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
ht.call(SOAP_ACTION, envelope);
detail = (SoapObject) envelope.getResponse();
parseWeather(detail);
} catch (Exception e) {
e.printStackTrace();
}
}
private void parseWeather(SoapObject detail)
throws UnsupportedEncodingException {
textview1 = (TextView) this.findViewById(R.id.TextView01);
String date = detail.getProperty(6).toString();
// 當天天氣
weatherToday = "\n天氣:" + date.split(" ")[1];
weatherToday = weatherToday + "\n氣溫:"
+ detail.getProperty(5).toString();
weatherToday = weatherToday + "\n風力:"
+ detail.getProperty(7).toString() + "\n";
weatherNow = detail.getProperty(8).toString();
weatherWillBe = detail.getProperty(9).toString();
textview1.setText(getIp() + '\n' + getCity() + "\n今天"
+ weatherToday);
setIcon(weatherNow, image1);
setIcon(weatherWillBe, image2);
}
5、最後在AndroidMainifest.xml加入權限
[java]
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
說明:由於筆者水平有限,難免會出現一些錯誤或漏洞,望讀者批評指正。
SeekBar 拖動條:拖動條和滾動條類似,當是拖動條可以拖動滑塊改變進度 RatingBar 星級評分條:星級評分條與拖動條相似 See
從本篇博客開始,我們開始分析PKMS的構造函數,看看PKMS到底是如何解析和管理手機中APK的信息的。由於PKMS的構造函數較長,我們會分段進行研究。public Pac
0.前言Android Studio目前已經成為開發Android的主要工具,作為開發者,調試、發現並解決BUG是家常便飯。正所謂,工欲善其事必先利其器,今天我們就來看看
轉載請注明出處,謝謝~~目錄本文概述 動畫補充說明 屬性動畫的View加載方式 TypeEvaluator的使用 TimeInterpolator LayoutTrans