Service实现轮询更新Ui

2017-05-11  本文已影响574人  heyzhuyue
     public class CityWeatherService extends Service  
    {  
            private static final int UPDATAWEATHER = 0X10;  
  
            private final int GOTOBROADCAST = 0X20;  
  
         public static final String BROADCASTACTION = "com.jone.broad";  
  
        Timer timer;  
  
  
         @Override  
         public IBinder onBind(Intent arg0)  
            {  
              // TODO Auto-generated method stub  
             return null;  
        }  
  
         @Override  
        public int onStartCommand(Intent intent, int flags, int startId)  
        {  
  
        // updateWeather();  
        timer = new Timer();  
        timer.schedule( new TimerTask()  
        {  
  
            @Override  
            public void run()  
            {  
                // 定时更新  
                String jsonString = getWeather();  
                // 发送广播  
                Intent intent = new Intent();  
                intent.setAction( BROADCASTACTION );  
                intent.putExtra( "jsonstr", jsonString );  
                sendBroadcast( intent );  
  
            }  
        }, 0, 20 * 1000 );  
  
        return super.onStartCommand( intent, flags, startId );  
            }  
        public class MainActivity extends Activity  
        {  
  
        @Override  
        protected void onCreate(Bundle savedInstanceState)  
         {  
            super.onCreate( savedInstanceState );  
            setContentView( R.layout.activity_main );  
            Intent intent = new Intent();  
            intent.setClass(this, CityWeatherService.class);  
            startService(intent);  
            broadcastMain = new BroadcastMain();  
            IntentFilter filter = new IntentFilter();  
            filter.addAction( CityWeatherService.BROADCASTACTION );  
            registerReceiver( broadcastMain, filter );  
          
        }  
  
        //接受Service发送的广播Receiver
        public class BroadcastMain extends BroadcastReceiver{  
        @Override  
        public void onReceive(Context context, Intent intent)  
        {  
            Message msg = handler.obtainMessage();  
            msg.what = 01;  
            handler.sendMessage( msg );  
            }  
        }
        
        //更新Ui的Handler
        Handler handler = new Handler()  
        {  
            public void handleMessage(android.os.Message msg)  
                {  
                 switch (msg.what)  
                {  
                 case 01:  
                    weather = manager.getWeather();  
                    cityTextView.setText( weather.getCityName() );  
                    break;  
  
                default:  
                    break;  
            }  
              
        };  
  
        };  
        @Override  
        protected void onDestroy()  
        {  
  
        Intent intent = new Intent();  
        intent.setClass(this, CityWeatherService.class);  
        stopService(intent);  
        super.onDestroy();  
            }  
        }  
上一篇下一篇

猜你喜欢

热点阅读