www.pudn.com > ctWeather.rar > ctWeather.java, change:2010-03-01,size:5963b


package com.android.ctbri.ctWeather; 
 
import android.app.PendingIntent;  
import android.app.Service;  
import android.appwidget.AppWidgetManager;  
import android.appwidget.AppWidgetProvider;  
import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.content.res.Resources;  
import android.net.Uri;  
import android.os.IBinder;  
import android.text.format.Time;  
import android.util.Log;  
import android.widget.RemoteViews;   
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
import org.apache.http.client.HttpClient; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.HttpResponse; 
import org.apache.http.HttpEntity; 
import java.io.InputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
 
 
public class ctWeather extends AppWidgetProvider { 
	private static byte[] sBuffer = new byte[512*16]; 
	 
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
        int[] appWidgetIds) { 
        // To prevent any ANR timeouts, we perform the update in a service 
        context.startService(new Intent(context, UpdateService.class)); 
    } 
     
    public void onReceive(Context context, Intent intent) { 
        // Protect against rogue update broadcasts (not really a security issue, 
        // just filter bad broacasts out so subclasses are less likely to crash). 
        String action = intent.getAction(); 
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) { 
        	context.startService(new Intent(context, UpdateService.class)); 
        } 
    } 
 
    public static class UpdateService extends Service { 
    	static int cnt = 0; 
        @Override 
        public void onStart(Intent intent, int startId) { 
            // Build the widget update for today 
            RemoteViews updateViews = buildUpdate(this); 
 
            // Push update for this widget to the home screen 
            ComponentName thisWidget = new ComponentName(this, ctWeather.class); 
            AppWidgetManager manager = AppWidgetManager.getInstance(this); 
            manager.updateAppWidget(thisWidget, updateViews); 
        } 
 
        public RemoteViews buildUpdate(Context context) { 
            // Pick out month names from resources 
            Resources res = context.getResources(); 
             
            RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_word); 
             
            Time tm = new Time(); 
            tm.setToNow(); 
            String showStr = ""; 
            if (tm.hour == 0) showStr += 12; 
            else { 
            	if (tm.hour < 10) showStr += 0; 
            	showStr += tm.hour; 
            } 
            showStr += ":"; 
            if (tm.minute < 10) showStr += 0; 
            showStr += tm.minute; showStr += ":"; 
            if (tm.second < 10) showStr += 0; 
            showStr += tm.second; 
             
            if (cnt == 0) { 
                String pageString = getHttpContent(); 
	            if (false) {//(pageString == "Failed!") { 
	            	showStr += "\n无可用天气信息\n请检查网络!"; 
	            } 
	            else { 
	            	String result = pageString.substring(pageString.indexOf("今日天气预报")); 
	             
		            String weather_head = "<em><strong>", weather_end = "</strong></em>"; 
		            String temperature_head = "<em class=\"no_today\">", temperature_end = "</em>"; 
		            String wind_head = "<em>", wind_end = "</em>"; 
		             
		            result = result.substring(result.indexOf(weather_head)); 
		            String weather = result.substring(weather_head.length(), result.indexOf(weather_end)); 
		            result = result.substring(result.indexOf(temperature_head)); 
		            String temperature = result.substring(temperature_head.length(), result.indexOf(temperature_end)); 
		            result = result.substring(result.indexOf(wind_head)); 
		            String wind = result.substring(wind_head.length(), result.indexOf(wind_end)); 
		            showStr += "\n" + weather + "," + wind + "\n" + temperature;  
		        } 
            } 
 
            updateViews.setTextViewText(R.id.time, showStr); 
	             
            return updateViews; 
        } 
 
        @Override 
        public IBinder onBind(Intent intent) { 
            // We don't need to bind to this service 
            return null; 
        } 
         
        private String getHttpContent() { 
            String url = "http://www.weather.com.cn/html/weather/101010100.shtml"; 
            HttpClient client = new DefaultHttpClient();          
            HttpGet request = new HttpGet(url);          
            request.setHeader("User-Agent", "hello"); 
 
            try { 
	            HttpResponse response = client.execute(request); 
	            // Pull content stream from response              
	            HttpEntity entity = response.getEntity();              
	            InputStream inputStream = entity.getContent();                           
	            ByteArrayOutputStream content = new ByteArrayOutputStream();                           
	            // Read response into a buffered stream              
	            int readBytes = 0;              
	            while ((readBytes = inputStream.read(sBuffer)) != -1) {                  
	            	content.write(sBuffer, 0, readBytes);              
	            	}                           
	            // Return result from buffered stream              
	            return new String(content.toByteArray()); 
            } catch (IOException e) {              
            	//throw new ApiException("Problem communicating with API", e);          
            }  
            return new String("Failed!"); 
        } 
    } 
}